You need to sign in to do that
Don't have an account?
Ben Merton 15
Test Class Newbie
I am trying to create a Test Class for this:
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?
//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; } }
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
Try the following:
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?
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