function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Matt MetrosMatt Metros 

Mock HTTP Call Out In Test to Return XML

Hello Everybody. I am very new to HTTP Callouts and now I am in the testing phase. My HTTP Callout returns XML

This is the link. I input a company already, soulcycle, in case anybody wanted to see what was being returned from the HTTP Request.

https://news.google.com/rss/search?q=soulcycle&hl=en-US&gl=US&ceid=US:en


Anyways, here is my Apex Class:
 
public with sharing class getRSSFeed {

	@AuraEnabled
	public static List<Map<String,String>> getCompanyRSSFeed(String url, String recordID) {
		
		String recordName = getRecordName(recordID);

		List<Map<String,String>> newsArticles = search(url, recordName);

		return newsArticles;
	}
	
	@AuraEnabled
	public static String getRecordName(String recordID)
	{
		String accountRecordID = recordID;
		return [SELECT Name From Account WHERE ID = : accountRecordID limit 1].Name;
	}

	@AuraEnabled
	public static  List<Map<String,String>> search(String remoteSiteURL, String query)
	{
		String url = remoteSiteURL + EncodingUtil.urlEncode(query, 'UTF-8');

		Http h = new Http();
	    HttpRequest req = new HttpRequest();
	    req.setEndpoint(url);
    	req.setMethod('GET');
	    HttpResponse res = h.send(req);

	    while (res.getStatusCode() == 302) {
		    req.setEndpoint(res.getHeader('Location'));
		    System.debug(res.getHeader('Location'));
		    res = new Http().send(req);
		}

	    Dom.Document doc = res.getBodyDocument();
	    Dom.XMLNode root = doc.getRootElement();

	   	Dom.XMLNode bodyNode = root.getChildElement('channel', null);

	   	//Dom.XMLNode bodyNode = bodyNode.getChildElement('');
		List<Map<String,String>> newsStories = new List<Map<String,String>>();
	   	Integer sizeOfList = 20;
	   	Integer i = 0;


	    for(DOM.XmlNode x : bodyNode.getChildren())
	    {
	    	//System.debug(x.getName());
	    	//System.debug(x.getText());
	    	if(x.getName() == 'item' &&
	    		i != sizeOfList)
	    	{
	    		Map<String,String> story = new Map<String, String>();
	    		story.put('publisher', '');
	    		story.put('date', '');
	    		story.put('link', '');
	    		story.put('image', '');
	    		story.put('title', '');
	    		story.put('description', '');


	    		for(DOM.XmlNode itemChildren : x.getChildElements())
	    		{
	    			System.debug(itemChildren.getName());

	    			//System.debug(itemChildren.getName());
	    			//System.debug(itemChildren.getText());
	    			if(itemChildren.getName() == 'link')
	    			{
	    				story.put('link', itemChildren.getText());
	    			}
	    			else if(itemChildren.getName() == 'title')
	    			{
	    				story.put('title', itemChildren.getText());
	    			}
	    			else if(itemChildren.getName() == 'pubDate')
	    			{
	    				story.put('date', itemChildren.getText());
	    			}
	    			else if(itemChildren.getName() == 'description')
	    			{

	    				String stripTags = itemChildren.getText();
	    				List<String> description = stripTags.split('(<.*?>)');
	    				story.put('description', description[5]);
	    			}
	    			else if(itemChildren.getName() == 'content')
	    			{
	    				story.put('image', itemChildren.getAttributeValue(itemChildren.getAttributeKeyAt(0), itemChildren.getAttributeKeyNsAt(0)));
	    			}
	    			else if(itemChildren.getName() == 'source')
	    			{
	    				story.put('publisher', itemChildren.getText());
	    			}


	    		}

	    		newsStories.add(story);
	    		i += 1;

	    	}

	    }
	    return newsStories;
	}


}

Here is my Test Class (Which is honestly embarrassing right now, because I have no clue what I am doing.)

 
@isTest
private class getRSSFeedTest {

	Public static final Integer numOfAccounts = 1;
	Public static final Integer numOfContacts = 1;
	
	@testsetup 
	public static void setupEnvironment()
	{
		createAccountsAndContacts(numOfAccounts,numOfContacts);
	}
	
	@isTest static void testAccountNews() 
	{
		String accId 	= [SELECT name from Account limit 1].id;
		String link		= 'https://news.google.com/rss/search?q=';

		 String accName = getRSSFeed.getRecordName(accId);
	 
	 	Test.setMock(HttpCalloutMock.class, new mockCallout());
	  	//getRSSFeed r = new getRSSFeed.search(link, 'Airbnb');
	 }


	

	public static void createAccountsAndContacts(Integer numberOfAccounts, Integer numberOfContactsPerAccount)
	 {
	   Database.DMLOptions dml = new Database.DMLOptions();
	   dml.DuplicateRuleHeader.allowSave = true;

	   List<Account> accountsToInsert = new List<Account>();

	   for(Integer i=0 ; i< numberOfAccounts; i++)
	   {

	     Account newAccount = new Account(
	           Name = 'Airbnb ' +  String.valueOf(i),
	           Industry = 'Accounting',
	           of_Locations__c = 5 + i
	      );

	     accountsToInsert.add(newAccount);
	   }

	   Database.insert(accountsToInsert, dml);


	   List<Contact> contactsToInsert = new List<Contact>();
	   for(Account acc: accountsToInsert)
	   {

	     for(Integer i = 0; i < numberOfContactsPerAccount; i++)
	     {
	       Contact newContact = new Contact(
	         lastName     = 'Test' + String.valueOf(i),
	         accountId   = acc.ID ,
	         title       = 'Test' ,
	         email       = String.valueOf(i)+'test@test.com',
	         Outreach_Status__c = 'Client'
	       );
	       contactsToInsert.add(newContact);
	     }

	   Database.insert(contactsToInsert, dml);

	   }
	 }  
}



I know this test class is really bad. I am researching a lot on how to achieve 100% code coverage. If you have any tips, please send them over. I appreciate all the help I can get!

Thanks in advance.