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
Akhil Katkam 5Akhil Katkam 5 

Test class for the below given code(imp)

Hi Developer Community , 


Can u please write test class for this apex code
public class RSSFeedUtil {
    public static List<RSSObject> getGoogleRSSObjects(String theUrl) {
        List<RSSObject> returnList = new List<RSSObject>();
        
        Http h = new Http();
        HttpRequest req = new HttpRequest();        
        req.setEndpoint(theUrl);
        req.setMethod('GET');
        HttpResponse res = h.send(req);
        
        Dom.Document doc = res.getBodyDocument();
        Dom.XMLNode feed = doc.getRootElement();
        String namespace = feed.getNamespace();
        
        for(Dom.XMLNode child : feed.getChildElements()) {
            if(child.getName() == 'entry') {
                RSSObject returnListItem = new RSSObject(
                    child.getChildElement('title', namespace).getText().unescapeHtml4(),
                    child.getChildElement('link', namespace).getAttribute('href', ''),                    
                    child.getChildElement('content', namespace).getText().unescapeHtml4(),
                    child.getChildElement('published', namespace).getText()
                );
                System.debug(returnListItem);
                returnList.add(returnListItem);
            }
            System.debug(returnList);
        }
        return returnList;        
    }
}

Thanks in Advance
Best Answer chosen by Akhil Katkam 5
Maharajan CMaharajan C
Hi Akhil,

For Http Callout first you have to create HttpCalloutMock class in your org then you need to refer that class in Test class.

1. Create the below Mock Apex class in your org:
 
@isTest
global class Test_RSSFeedUtilMockGenerator  implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest req) {
        HttpResponse res = new HttpResponse();
        String xml = '<entries>' + '<entry>' +
            '<title>Salesforce</title>' +
            '<link>https://www.google.com</link>' +
            '<content>Test</content>' +
            '<published>2021</published>' +
            '</entry>' + '</entries>';
        res.setHeader('Content-Type', 'application/xml');
        res.setBody(xml);
        res.setStatusCode(200);
        return res;
    }
}

2. Apex Test class:
@isTest
public class RSSFeedUtilTest {
    @isTest static void getGoogleRSSObjects(){
        Test.setMock(HttpCalloutMock.class, new Test_RSSFeedUtilMockGenerator()); 
        Test.startTest();
        List<RSSObject> res = RSSFeedUtil.getGoogleRSSObjects('https://www.google.com');
        Test.stopTest();
    }
}

Thanks.
Maharajan.C

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi,

You have to make mockUp data to cover HTTP request test class, to get a fake response.
please follow the below link to create mockup class:-
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm


Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi
Maharajan CMaharajan C
Hi Akhil,

Please post the sample response and Endpoint URL so that we can easily help you on the mock and test class.

Thanks,
Maahrajan.C
Akhil Katkam 5Akhil Katkam 5
Hi suraj tripathi and maharajan ,thanks for the replies  ,
http  related code  is being covered , i just need  test class from 
Dom.Document doc = res.getBodyDocument();
can u please help me test class for red lines that need to be covered

 User-added image


thanks in advance
Maharajan CMaharajan C
Hi Akhil,

For Http Callout first you have to create HttpCalloutMock class in your org then you need to refer that class in Test class.

1. Create the below Mock Apex class in your org:
 
@isTest
global class Test_RSSFeedUtilMockGenerator  implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest req) {
        HttpResponse res = new HttpResponse();
        String xml = '<entries>' + '<entry>' +
            '<title>Salesforce</title>' +
            '<link>https://www.google.com</link>' +
            '<content>Test</content>' +
            '<published>2021</published>' +
            '</entry>' + '</entries>';
        res.setHeader('Content-Type', 'application/xml');
        res.setBody(xml);
        res.setStatusCode(200);
        return res;
    }
}

2. Apex Test class:
@isTest
public class RSSFeedUtilTest {
    @isTest static void getGoogleRSSObjects(){
        Test.setMock(HttpCalloutMock.class, new Test_RSSFeedUtilMockGenerator()); 
        Test.startTest();
        List<RSSObject> res = RSSFeedUtil.getGoogleRSSObjects('https://www.google.com');
        Test.stopTest();
    }
}

Thanks.
Maharajan.C
This was selected as the best answer
Akhil Katkam 5Akhil Katkam 5
Thank you so much Maharajan , code coverage has been completed with 100%.
Baya AdamBaya Adam
Information Transformation Service provides  web scraping Services that provides high-quality structured data to improve business outcomes and enable intelligent decision making,their Web scraping service allows you to scrape data from any websites and transfer web pages into an easy-to-use format such as Excel, CSV, JSON and many others.
 
Barbara Smith 45Barbara Smith 45
Hi, I want to tell you about a site that writes cool essays. I hope this information was useful to you! More information - read more (https://www.privatewriting.com/) . Our writers have access to up-to-date, relevant sources and can create outstanding research essay on any topic. Have you ever found yourself in a similar situation? Keep calm, there is no need to panic and stress out.
dfsbvf fvdsbfvddfsbvf fvdsbfvd
Yes I am also looking for a similar type of project and need help to run my website (https://batteriadipentole.com/) you can see here the details of my site.
Emma JacksonEmma Jackson
Thank you so much for sharing this kind of interesting article with us. Check this Red Suit (https://www.thegenuineleather.com/product/joker-joaquin-phoenix-red-suit/). I will be waiting for your others articles.
 
hgfjnh hgfjhfhgfjnh hgfjhf
The code looks easy to appy and the class I mentioned here on grease (https://greasetrapguy.com/best-domestic-grease-traps/) you can see the class tips and apply it.
Puja Agarwal 63Puja Agarwal 63
You look awesome. I couldn’t help but share my thoughts.
Kolkata FF (https://kolkata-ff.net)
Puja Agarwal 63Puja Agarwal 63
Why wasn’t I invited for the snap then? This is a perfect example of a quality portrait of a quality human being.
Nagaland State Lottery (https://nagalandstatelotterysambad.com)
Sam Smith 70Sam Smith 70
A capstone project is that very document that will represent your scientific investigations to the examination board ( http://essayswriting.services/ ). Generally, it’s obligatory for almost all academic directions, but capstone preparation is in high demand more for medical, engineering, IT, and business programs.
Naeem AliNaeem Ali
wow! your content is very helpful for us such a nicer article provide I really impressed with this stuff.
brown leather jacket womens (https://glorystore.com.au/product/womens-chocolate-brown-leather-biker-jacket/)
Naeem AliNaeem Ali

thanks you for a such awesome article.
shearling coat mens (https://glorystore.uk/collections/men-shearling-longcoat)