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
Prudhvi Paladugu 2Prudhvi Paladugu 2 

Hi, I need help with test coverage for below class.

Class -
public with sharing class CaseContractPageController {

     
       public string selectedRecordType {get;set;}
       
       
    public CaseContractPageController(ApexPages.StandardController controller) {

    }
   
   
   public pagereference newPage(){
   
   
   recordType rec = [select id from RecordType where developerName=: selectedRecordType AND sObjectType ='Case'] ;
   
   PageReference pageRef = new PageReference('/500/e?retURL=%2F500%2Fo&RecordType='+rec.id+'&ent=Case');
    pageRef.setredirect(true);  
    return pageRef;
   
   
   }
   
    public pagereference cancelPage(){
   
   PageReference pageRef = new PageReference('https://ctsfullcrm.comcastctsfull.shnpoc.net');
    pageRef.setredirect(true);  
    return pageRef;
   
   
   }
   
   
   }

Test Class-

    static testMethod void newPageTestMethod() {
    
    Account recAccount = new Account(Name='TestAccount');
    Insert recAccount;
    Case recCase = new Case(AccountId=recAccount.Id, RecordTypeId ='012q00000009TUM');
    insert recCase;

   
    Apexpages.standardcontroller sc = new Apexpages.standardcontroller(recCase);
    CaseContractPageController cseController = new CaseContractPageController (sc);
         Id  recordTypeId = [select id from RecordType where Name='LR - NDA'].id;
    
    cseController.selectedRecordType  = 'LR - NDA';
   
    pagereference pagecase = Page.CaseContract;
    Test.setcurrentpage(pagecase);

    system.debug('recordTypeIdTest'+recordTypeId );
    

    pagecase.getParameters().put('rec.id',recordTypeId );

 
 try{
    cseController.newPage();
    }catch(Exception e){}
  
    }



 
Best Answer chosen by Prudhvi Paladugu 2
Raj VakatiRaj Vakati
Here is the working copy . Check LR_NDA Record type name and developer name once .. 

 
@istest  
private class HelloWorldTestClass {
    
    static testMethod void newPageTestMethod() {
        
        Account recAccount = new Account(Name='TestAccount');
        Insert recAccount;
        
        
        Id recId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('LR_NDA').getRecordTypeId();
        
        
        
        Case recCase = new Case(AccountId=recAccount.Id, RecordTypeId =recId);
        insert recCase;
        
        
        Apexpages.standardcontroller sc = new Apexpages.standardcontroller(recCase);
        CaseContractPageController cseController = new CaseContractPageController (sc);
        
        pagereference pagecase = Page.CaseContract;
        Test.setcurrentpage(pagecase);
        
        
        pagecase.getParameters().put('rec.id',recId );
        cseController.selectedRecordType = 'LR_NDA';
        cseController.newPage(); 
        cseController.cancelPage(); 
        
        
        
    }
}

 

All Answers

Raj VakatiRaj Vakati
@istest  
  private class HelloWorldTestClass {

  static testMethod void newPageTestMethod() {
    
    Account recAccount = new Account(Name='TestAccount');
    Insert recAccount;


	Id stExam = Schema.SObjectType.Student_Records__c.getRecordTypeInfosByName().get('LR - NDA').getRecordTypeId();

	
	
    Case recCase = new Case(AccountId=recAccount.Id, RecordTypeId =stExam);
    insert recCase;

   
    Apexpages.standardcontroller sc = new Apexpages.standardcontroller(recCase);
    CaseContractPageController cseController = new CaseContractPageController (sc);
        
    pagereference pagecase = Page.CaseContract;
    Test.setcurrentpage(pagecase);

   
    pagecase.getParameters().put('rec.id',recordTypeId );
cseController.newPage(); 
cseController.cancelPage(); 

 
  
    }
	}

 
Prudhvi Paladugu 2Prudhvi Paladugu 2
Hi Raj, Thanks for your reply but I am getting error. Also, even in my test class I am passing the rec.id with record type id. I am able to cover for cancelPage method but not able to for newPage(). 
Raj VakatiRaj Vakati
what is the error you are getting 
Prudhvi Paladugu 2Prudhvi Paladugu 2
Error -  List has no rows for assignment to SObject​
Raj VakatiRaj Vakati
Here is the working copy . Check LR_NDA Record type name and developer name once .. 

 
@istest  
private class HelloWorldTestClass {
    
    static testMethod void newPageTestMethod() {
        
        Account recAccount = new Account(Name='TestAccount');
        Insert recAccount;
        
        
        Id recId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('LR_NDA').getRecordTypeId();
        
        
        
        Case recCase = new Case(AccountId=recAccount.Id, RecordTypeId =recId);
        insert recCase;
        
        
        Apexpages.standardcontroller sc = new Apexpages.standardcontroller(recCase);
        CaseContractPageController cseController = new CaseContractPageController (sc);
        
        pagereference pagecase = Page.CaseContract;
        Test.setcurrentpage(pagecase);
        
        
        pagecase.getParameters().put('rec.id',recId );
        cseController.selectedRecordType = 'LR_NDA';
        cseController.newPage(); 
        cseController.cancelPage(); 
        
        
        
    }
}

 
This was selected as the best answer
Prudhvi Paladugu 2Prudhvi Paladugu 2
I was able to get the code coverage, thanks a lot for the help!