0

I am attempting to write a test class for a very basic controller extension. I cannot seem to get 100% coverage in this method. From the 'Try' statement to the 'Else' statement has no coverage. Any help is greatly appreciated.

public pageReference save() {
    if(c.origin_postal_code__c != null || c.destination_postal_code__c != null){
        try{    //NO CODE COVERAGE STARTS HERE.
            insert c;
            apexpages.addmessage(new apexpages.message(apexpages.severity.CONFIRM,'The record has been added'));
        }
        catch(exception e){
            system.debug('The following exception has occurred ' + e.getMessage());
        }
        string acc = c.account__c;
        string con = c.contact__c;
        string opp = c.opportunity__c;

        c = new custom_object__c();
        c.account__c = acc;
        c.contact__c = con;
        c.opportunity__c = opp;
        c.status__c = 'Pending'; //NO CODE COVERAGE ENDS HERE.
    }
    else{
        apexpages.addmessage(new apexpages.message(apexpages.severity.ERROR,'Please enter both zip codes to submit the record'));
    }      
    return null;
}

My test class is as follows:

    @istest (seealldata = true)
    public class TestEntryTest{
        public static TestMethod Void Test1(){

test.starttest();
        Test.setCurrentPage(page.record_entry);
            ApexPages.StandardController qe = new ApexPages.standardController(new custom_object__c());
            record_entry_extension controller = new record_entry_extension(qe);
            controller.save();
            test.stoptest();  

        }

    }

1 Answer 1

1

As the code you are testing includes this condition:

if(c.origin_postal_code__c != null || c.destination_postal_code__c != null){

you need to ensure that your test creates an object with values that make that true.

One way to do that is this:

ApexPages.StandardController qe = new ApexPages.standardController(new custom_object__c(
        origin_postal_code__c = 'ABC',
        destination_postal_code__c = 'XYZ'
        ));
1
  • Never thought to satisfy the if condition. Thank you very much for sharing your expertise. Greatly appreciated.
    – Berg
    Commented May 30, 2014 at 18:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .