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 this RSSFEEDCTRL

Hi Developer community , 

i have an apex class , i am having only little knowledge on test classes , 
can u please write test class for below apex code
public class RSSFeedCtrl {
    @AuraEnabled
    public static String getURL(String URLField, ID recordId, String objectName) {
        String queryString = 'select ' + URLField + ' from ' + objectName + ' where Id = \'' + recordId + '\'';
        String returnValue = '';
        
        try {
            sObject s = Database.query(queryString);
            returnValue = (String)s.get(URLField);
        }
        catch (Exception e) {
            throw new AuraHandledException('Error in getURL method of RSSFeedCtrl: ' + e.getMessage());    
        }
        
        return returnValue;
    }

    @AuraEnabled
    public static List<RSSObject> getRSSFeed(String url) {
        List<RSSObject> toReturn = new List<RSSObject>();
        try {
            if(url != null){
                toReturn = RSSFeedUtil.getGoogleRSSObjects(url);
            } 
        }
        catch (Exception e) {
            throw new AuraHandledException('Error in getRSSFeed method of RSSFeedCtrl:' + e.getMessage());    
        }
               
        return toReturn;
    }
}

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

Before use the below test class you need the mock class i have already posted in your another query. The same mock class am posting again. If you aleady created then don't create the below class in your org otherwise you have to create it.
 
@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;
    }
}
 

Please try the below test class:
 
@istest
Public class RSSFeedCtrl_Test{
    @istest static void method1(){
        Account acc= new Account();
        acc.name='Test';
        insert acc;
        
        try
        {
            RSSFeedCtrl.getURL('website',acc.id,'Account');
            RSSFeedCtrl.getRSSFeed('https://www.google.com/');
        }
        catch(Exception e) 
        {
        }
    }
    
    @istest static void method2(){
        Account acc= new Account();
        acc.name='Test';
        insert acc;
        
        Test.setMock(HttpCalloutMock.class, new Test_RSSFeedUtilMockGenerator()); 
        Test.startTest();
        RSSFeedCtrl.getRSSFeed('https://www.google.com/');
        Test.stopTest();
        try
        {
            RSSFeedCtrl.getURL('Test__c',acc.id,'Account'); 
        }
        catch(Exception e) 
        {
        }
    }
}

Thanks,
Maharajan.C

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Akhil,

You can take reference from this below code.
@istest
Public class RSSFeedCtrl_Test{
  @istest static void method(){
     Account acc= new Account();
     acc.name='Test';
     insert acc;
    
    test.starttest();
    RSSFeedCtrl.getURL('name',acc.id,'Account');
    RSSFeedCtrl.getRSSFeed('https://www.google.com/');
   test.stoptest();
   }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
Akhil Katkam 5Akhil Katkam 5
Hi suraj , it is not covering 3 lines of code , can u please check that   
User-added image

Thanks in advance
Akhil Katkam 5Akhil Katkam 5
it is showing as :

System.AuraHandledException: Script-thrown exception
Suraj Tripathi 47Suraj Tripathi 47
Hi Akhil,
Try this test class:-
@istest
Public class RSSFeedCtrl_Test{
  @istest static void method(){
     Account acc= new Account();
     acc.name='Test';
     insert acc;
    
    test.starttest();
    RSSFeedCtrl.getURL('name',acc.id,'Account');
    RSSFeedCtrl.getURL('name',acc.id,'Contact');
    RSSFeedCtrl.getRSSFeed('https://www.google.com/');
   test.stoptest();
   }
}

 
Suraj Tripathi 47Suraj Tripathi 47
Hi Akhil

If you want to cover catch block then you are getting this error.
Thanks
Akhil Katkam 5Akhil Katkam 5
hi suraj , can u please tell me how to solve the catch block issue , and the code which u sent second time is only covering 50% 

User-added image

can u please solve this issue
Maharajan CMaharajan C
Hi Akhil,

You can use the try catch in test class to avoid this error  System.AuraHandledException: Script-thrown exception

Try the below test class:
 
@istest
Public class RSSFeedCtrl_Test{
  @istest static void method(){
	Account acc= new Account();
	acc.name='Test';
	insert acc;
    
    test.starttest()
		try
		{
			RSSFeedCtrl.getURL('website',acc.id,'Account');
			RSSFeedCtrl.getRSSFeed('https://www.google.com/');
            RSSFeedCtrl.getURL('Test__c',acc.id,'Account'); 
		}
		catch(Exception e) 
		{
		}
	test.stoptest();
   }
}

Thanks,
Maharajan.C
​​​​​​​
Akhil Katkam 5Akhil Katkam 5
Hi Maharajan ,thanks for the reply , 
the catch exception is not covering still and also there is one more line return code also didint cover 

could u please tell me solution for this

User-added image

thanks in advance
Maharajan CMaharajan C
Hi Akhil,

Before use the below test class you need the mock class i have already posted in your another query. The same mock class am posting again. If you aleady created then don't create the below class in your org otherwise you have to create it.
 
@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;
    }
}
 

Please try the below test class:
 
@istest
Public class RSSFeedCtrl_Test{
    @istest static void method1(){
        Account acc= new Account();
        acc.name='Test';
        insert acc;
        
        try
        {
            RSSFeedCtrl.getURL('website',acc.id,'Account');
            RSSFeedCtrl.getRSSFeed('https://www.google.com/');
        }
        catch(Exception e) 
        {
        }
    }
    
    @istest static void method2(){
        Account acc= new Account();
        acc.name='Test';
        insert acc;
        
        Test.setMock(HttpCalloutMock.class, new Test_RSSFeedUtilMockGenerator()); 
        Test.startTest();
        RSSFeedCtrl.getRSSFeed('https://www.google.com/');
        Test.stopTest();
        try
        {
            RSSFeedCtrl.getURL('Test__c',acc.id,'Account'); 
        }
        catch(Exception e) 
        {
        }
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Akhil Katkam 5Akhil Katkam 5
Thank you Maharajan , succesfully solved the issue