+ Start a Discussion
Ben Merton 15Ben Merton 15 

Test Class Newbie

I am trying to create a Test Class for this:
 
//This class is used as a controller for allowing the Divisions tab to land directly on the List View

public class DivisionsTabOverride{
public PageReference exit(){
//change the Any_ObjectName__c with your Custom or Standard Object name.
Schema.DescribeSObjectResult anySObjectSchema = Division__c.SObjectType.getDescribe();
String objectIdPrefix = anySObjectSchema.getKeyPrefix();
PageReference pageReference = new PageReference('/'+objectIdPrefix);
pageReference.setRedirect(true);
return pageReference;
}
}

This is what I have so far.  It passes the test when I press run, but I am unable to see any corresponding code coverage change in the class above.  Also, please let me know if I need to add anything to this test to get coverage?
 
@isTest
//This class is used as a controller for allowing the Divisions tab to land directly on the List View

public class TestDivisionsTabOverride{
static testmethod PageReference exit(){
//change the Any_ObjectName__c with your Custom or Standard Object name.
Schema.DescribeSObjectResult anySObjectSchema = Division__c.SObjectType.getDescribe();
String objectIdPrefix = anySObjectSchema.getKeyPrefix();
System.assert(objectIdPrefix.length()==3);

PageReference pageReference = new PageReference('/'+objectIdPrefix);
pageReference.setRedirect(true);
return pageReference;

}
}

 
Best Answer chosen by Ben Merton 15
James LoghryJames Loghry
Ben,

A small typo on my part. It should be:

System.assertEquals('/'+objectIdPrefix,dto.exit().getUrl());

More on the PageReference methods here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_System_PageReference_methods.htm

All Answers

James LoghryJames Loghry
Your class is not covered, because you are never calling the class!

Try the following:
 
@isTest
private class TestDivisionsTabOverride{
    static testmethod PageReference exit(){
        Schema.DescribeSObjectResult anySObjectSchema = Division__c.SObjectType.getDescribe();
        String objectIdPrefix = anySObjectSchema.getKeyPrefix();
        //Instantiate the DivisionsTabOverride class first, using the default or blank constructor.
        DivisionsTabOverride dto = new DivisionsTabOverride();
        //Call the instance method "exit", and assert that it's returning the correct link.
        System.assertEquals('/'+objectIdPrefix,dto.exit());
    }
}

 
Ben Merton 15Ben Merton 15
Okay that makes sense but I am getting the following error:

Error: Compile Error: Comparison arguments must be compatible types: String, System.PageReference at line 9 column 9

Forgive me for being an Apex newbie but I am assuming this means that System.PageReference isn't a string that matches dto.exit.  But because dto.exit() returns the PageReference with the ('/'+objectIdPrefix) argument.  Surely both are therefore strings?  
James LoghryJames Loghry
Ben,

A small typo on my part. It should be:

System.assertEquals('/'+objectIdPrefix,dto.exit().getUrl());

More on the PageReference methods here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_System_PageReference_methods.htm
This was selected as the best answer