+ Start a Discussion

Testing PageReference



I am trying to increase my code coverage from 85% to 100%. The only section that is in red from the test result is the pagereference section. Here is my code:


public class exNewFeedback {

    public Case requests {get; set;}
    public String details {get; set;}
    public String fbFrom {get; set;}
    public String fbSource {get; set;}
    Feedback__c feedback;
    public exNewFeedback(ApexPages.StandardController controller) {
        requests = (Case)controller.getRecord();
    public PageReference createRequest(){
        Feedback__c compliment = new Feedback__c();
        compliment.Member__c = requests.Contact.Account.Id;
        compliment.Request__c = requests.ID;
        compliment.Supplier__c = requests.Supplier__c;
        compliment.Escalated_to_Complaint__c = 'No';
        compliment.Relevant_office__c = requests.User_Owner_Office__c;
        compliment.Details__c = details;
        compliment.Feedback_From__c = fbFrom;
        insert compliment;
        PageReference fbPage = new ApexPages.StandardController(compliment).view();
        return fbPage;
    static testMethod void testNewFeedback(){
           Account member = new Account(Name = 'Test User');
           insert member;
           LIST<Account> memberlist = [SELECT ID FROM Account LIMIT 1];
           System.assertEquals(1, memberlist.size());
           Supplier__c supplier = new Supplier__c(Name = 'Test Supplier', Status__c = 'Info Only', Category__c = 'Beauty', Type__c = 'Nails');
           insert supplier;
           LIST<Supplier__c> supplierlist = [SELECT ID FROM Supplier__c LIMIT 1];
           System.assertEquals(1, supplierlist.size());
           Case request = new Case(Origin = 'Email', Status = 'Open', Supplier__c = supplierlist[0].ID);
           insert request;
           LIST<Case> requestlist = [SELECT ID FROM Case LIMIT 1];
           System.assertEquals(1, requestlist.size());
           Apexpages.Standardcontroller stdCon = new apexpages.Standardcontroller(request);
           exNewFeedback exFB = new exNewFeedback (stdCon);

Please help. Thank you.

James LoghryJames Loghry



Looking at your logic, the unit test should hit the "createRequest" method in your class, and all the code associated with it.


It may be due to a bug I've seen where code coverage is calculated based on cached test results.


E.g. you may have to go to Setup->Develop->Apex test execution and click "View test history" and then click "Clear results"


After you run all tests, your code coverage should be more accurate.


Also, taking a look at your unit test, I would suggest setting the createRequest method to a PageReference variable and doing some checks or System.assert logic against it as well.


Hope that helps.