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
SurenderSurender 

Need Test Class for the ABC Apex class

Hi,

 

How to write Test Case for the below Apex class..

 

public class ABC {

    public PageReference callfun() {
        Boolean flag= false;
        
        User usr= [SELECT Use_Policy__c from User where Id=:UserInfo.getUserId() and IsActive=true];
        
        if(usr.Use_Policy__c) {
            PageReference pageRef = Page.UsePolicySuccess;
            pageRef.setRedirect(true);
            return pageRef;
        } else{
        
           PageReference pageRef = Page.UsePolicyPage;
           pageRef.setRedirect(true);
           return pageRef;       
        }
    }

}

 

Your feedback is appreciated..

Navatar_DbSupNavatar_DbSup

Hi,

 

your class with test mtehod 

public class ABC {

    public PageReference callfun() {
        Boolean flag= false;
        
        User usr= [SELECT Use_Policy__c from User where Id=:UserInfo.getUserId() and IsActive=true];        
         system.debug('aaaaaaaaaaaaaaa'+usr);
        if(usr.Use_Policy__c) {
            PageReference pageRef = Page.UsePolicySuccess;
            pageRef.setRedirect(true);
            return pageRef;
        } else{
        
           PageReference pageRef = Page.UsePolicyPage;
           pageRef.setRedirect(true);
           return pageRef;       
        }
    }
    
    public static testMethod void testMethod1()
     {
         
         ABC abcobj=new ABC();
         abcobj.callfun();
    }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

SurenderSurender

But every time test coverage executed for positive path. how to make it to execute else part.

 

Andy BoettcherAndy Boettcher

Best practice is to always stage your data in a Test Method - and to never statically declare variables within a Method.

 

If you did something like this:

 

public class ABC {

    public User usr = [SELECT Use_Policy__c from User where Id=:UserInfo.getUserId() and IsActive=true];

    public PageReference callfun() {
        Boolean flag= false;
             
	// REMOVE THE LINE THAT CREATES THE USR OBJECT FROM HERE
        system.debug('aaaaaaaaaaaaaaa'+usr);

        if(usr.Use_Policy__c) {
            PageReference pageRef = Page.UsePolicySuccess;
            pageRef.setRedirect(true);
            return pageRef;
        } else{
        
           PageReference pageRef = Page.UsePolicyPage;
           pageRef.setRedirect(true);
           return pageRef;       
        }
    }
    
    public static testMethod void testMethod1()
     {
        
	// Instanciate Object 
	ABC abcobj=new ABC();

	test.startTest();

	// Prep Variables
	Profile p = [select id from profile where name='Standard User'];


	// Stage Data for Positive Path
	User uPos = new User(alias = 'standt', email='standarduser@testorg.com',
			emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
			localesidkey='en_US', profileid = p.Id,timezonesidkey='America/Los_Angeles', 
			username='standarduser@testorg.com', Use_Policy__c = true);
	insert uPos;

	// Positive Path TEST
	abcobj.usr = uPos;
        abcobj.callfun();

	// Stage Data for Negative Path
	User uNeg = new User(alias = 'standt2', email='standarduser2@testorg.com',
			emailencodingkey='UTF-8', lastname='Testing2', languagelocalekey='en_US',
			localesidkey='en_US', profileid = p.Id,timezonesidkey='America/Los_Angeles', 
			username='standarduser2@testorg.com', Use_Policy__c = false);
	insert uNeg;
	
	// Negative Path TEST
	abcobj.usr = uNeg;
        abcobj.callfun();

	test.stopTest();

    }

}