+ Start a Discussion
TemesgenTemesgen 

Test coverage for url in Pagereference method with if statement

Hi everyone!

I am trying to test this code to obtain 100% coverage, but i can't figure out the problem. When i test it, I don't get an error but the code is still not 100%, kindly seeking your help, Thank you!


Code:
public Pagereference newPage() {
        Id cohortRecordTypeId = Schema.SObjectType.class__c.getRecordTypeInfosByName().get('Cohort').getRecordTypeId();
        String url = 'https://bitwisecommunity.force.com/instructorportal/CohortianRoster?id=' + classId;
        if(classType == cohortRecordTypeId) {
            url += '&type=Cohort';
            
        } 
        
        return new PageReference(url);
    }


Test Class: 
static testMethod void testPagereference(){
        
        class__c newClass = new class__c(name = 'Test Class', class_nights__c = 'Monday / Wednesday', start_date__c = date.today() - 10, end_date__c = date.today() + 45);
        insert newClass;
        Test.startTest();
        // String cohRecordType = Schema.SObjectType.class__c.getRecordTypeInfosByName().get('Cohort').getRecordTypeId();
        PageReference pref = Page.CohortianSelector;
        

        Test.setCurrentPage(pref);
        pref.getParameters().put('id',newClass.Id);
        ApexPages.StandardController sc = new ApexPages.StandardController(newClass);
        CohortianSelectorController myPage = new CohortianSelectorController(sc);
        myPage.newPage();
        Test.stopTest();
}

Best Answer chosen by Temesgen
Maharajan CMaharajan C
Hi,

1. Insert the class__c record in Test class with record type:
   
String cohRecordType = Schema.SObjectType.class__c.getRecordTypeInfosByName().get('Cohort').getRecordTypeId();
class__c newClass = new class__c(name = 'Test Class', class_nights__c = 'Monday / Wednesday', start_date__c = date.today() - 10,end_date__c = date.today() + 45, RecordTypeId = cohRecordType);
insert newClass;

2.  or classType  is local property not set by the class__c record in controller. then you can set the value from test class directly like below.
String cohRecordType = Schema.SObjectType.class__c.getRecordTypeInfosByName().get('Cohort').getRecordTypeId();
CohortianSelectorController myPage = new CohortianSelectorController(sc);
myPage.classType  =  cohRecordType;
myPage.newPage();


Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi,

1. Insert the class__c record in Test class with record type:
   
String cohRecordType = Schema.SObjectType.class__c.getRecordTypeInfosByName().get('Cohort').getRecordTypeId();
class__c newClass = new class__c(name = 'Test Class', class_nights__c = 'Monday / Wednesday', start_date__c = date.today() - 10,end_date__c = date.today() + 45, RecordTypeId = cohRecordType);
insert newClass;

2.  or classType  is local property not set by the class__c record in controller. then you can set the value from test class directly like below.
String cohRecordType = Schema.SObjectType.class__c.getRecordTypeInfosByName().get('Cohort').getRecordTypeId();
CohortianSelectorController myPage = new CohortianSelectorController(sc);
myPage.classType  =  cohRecordType;
myPage.newPage();


Thanks,
Maharajan.C
This was selected as the best answer
TemesgenTemesgen
It worked, Thank you very much Maharajan!