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
Prady01Prady01 

Test class for 100% coverage

Hello there people i have a simple class for which i am working on a test class and cant seem to figure out to pass the record id... The whole function is i have page with two page blocksections and then pass the boolean value from class to page so that for certain record type only releted page blocksection should be visible... Here is the piece of class and i have marked the lines that was not covered by test

public class cont{

 public boolean boolval{get;set;}
 
    public cont(ApexPages.StandardController controller) {
        customobject pr = (customobject)controller.getRecord();
    
 
		if(ApexPages.currentPage().getParameters().get('id')!=null)
		{
			customobject  pr1 = [select id, RecordTypeId from customobject where id =  :ApexPages.currentPage().getParameters().get('id')];  
			if  (pr1.RecordTypeId == 'ID of the record type/or label')
			{
				boolval=true;
				          
			} 
			else 
			{
			boolval=false; // not covered in test
			}
        }
		
	    else if(ApexPages.currentPage().getParameters().get('RecordType')== 'ID of the record type/or label')
        boolval=true; // not covered in test
        else 
		boolval=false;
	}
 
}
@isTest
private class contTest {


        static testMethod void testcont()
        {        
            
         customobject pr = new customobject();
         insert pr;
          PageReference pageRef = Page.custompage;
          pageRef .getParameters().put('id',pr.id);
         
           Test.setCurrentPageReference(pageRef);
           ApexPages.StandardController controller = new ApexPages.StandardController(pr); 
           PerformanceReviewController cont = new PerformanceReviewController(controller);  
         
       
           
            customobject pr1 = new customobject();
          
           PageReference pageRef1 = Page.custompage;
          pageRef1 .getParameters().put('RecordTypeid','0124000000015Sq');
       
          Test.setCurrentPageReference(pageRef1);
           ApexPages.StandardController controller1 = new ApexPages.StandardController(pr1); 
           PerformanceReviewController cont1 = new PerformanceReviewController(controller1);  
           
           
         }     
         
    } 

 

as " not covered ".... Thanks in advance if ayone would help me out... Any help would be deeply

Appreciated :smileyhappy :)

Best Answer chosen by Prady01
bob_buzzardbob_buzzard

That's not a late reply - sometimes I pitch up after a year if I notice something kicking around my email folders :)

 

If that's the exact code, you haven't quite passed the parameters correctly - you are passing string literals rather than the field from the recordtype objects. Also, your code is looking for the parameter 'RecordType' rather than 'RecordTypeId'.

 

You need to remove the single quotes from the second parameter passed to put, e.g.

 

pageRef1 .getParameters().put('RecordType',recordtype1.id);

All Answers

bob_buzzardbob_buzzard

The line of code that you have marked as not covered will only be executed if there is an ID passed via the URL and the record type id of the custom object doesn't equal the hardcoded record type id.

 

In your test class you are only passing one id to the controller, presumably one that does match the hardcoded id.

Prady01Prady01

Really Sorry for the super late reply... 

 

Hello bob_buzzard first of all thanks a lot for replying to my post :) I appreciate your help..... Actually i have two questions for you

1) the test class is syntactically  and semantically correct for the apex class?? Because i tried the same things with different record type id and still i couldnt cover the code completely, For both the record type id i passed i could see the same lines not covered and i used different record types inside this parameter

pageRef1 .getParameters().put('RecordTypeid','recordtype1.id');
pageRef1 .getParameters().put('RecordTypeid','recordtype2.id');

..

 

2) And have i passed the recordy type id correctly in my test class??

 

 

Thanks in advance...

bob_buzzardbob_buzzard

That's not a late reply - sometimes I pitch up after a year if I notice something kicking around my email folders :)

 

If that's the exact code, you haven't quite passed the parameters correctly - you are passing string literals rather than the field from the recordtype objects. Also, your code is looking for the parameter 'RecordType' rather than 'RecordTypeId'.

 

You need to remove the single quotes from the second parameter passed to put, e.g.

 

pageRef1 .getParameters().put('RecordType',recordtype1.id);
This was selected as the best answer