+ Start a Discussion
ckellieckellie 

Test Code Coverage 43% - Not covering the if statement

I am writing a test on the a class where about half the code is inside the classes if statement. How do I cover the if statement?

 

Here is the test code:

@IsTest private class TestQuoteExtend {

    /* This is a basic test which simulates the primary positive case for the 
       save method in the quoteExt class. */
    public static testmethod void basicSaveTest() {
        
        Opportunity o = TestQuoteExtend.setupTestOpportunity();
        
        /* Construct the standard controller for quote. */
        ApexPages.StandardController con = new ApexPages.StandardController(new quote());


        /* Switch to runtime context */
        Test.startTest();

        /* Construct the quoteExt class */
        QuoteExtend ext = new QuoteExtend(con);
        
          PageReference result = ext.save();
        
        /* Switch back to test context */
        Test.stopTest();

    }
        /* Construct the standard controller for quote. */
        ApexPages.StandardController con = new ApexPages.StandardController(new Quote());

        /* Construct the quoteExt class */
        QuoteExt ext = new QuoteExt(con);

        /* Call save on the ext */
        //ext.save();       

    /* This setup method will create an opportunity with line items and a primary
       contact role for use in various tests. */
    private static Opportunity setupTestOpportunity() {

        /* Create an account */
        Account a = new Account();
        a.name    = 'TEST';
        Database.insert(a);

        /* Get the standard pricebook. There must be a standard pricebook already 
           in the target org.  */
        Pricebook2 pb = [select name, isactive from Pricebook2 where IsStandard = true limit 1];

        if(!pb.isactive) {
            pb.isactive = true;
            Database.update(pb);
        }

        /* Get a valid stage name */
        OpportunityStage stage = [select MasterLabel from OpportunityStage limit 1];

        /* Setup a basic opportunity */
        Opportunity o  = new Opportunity();
        o.Name         = 'TEST';
        o.AccountId    = a.id;
        o.CloseDate    = Date.today();
        o.StageName    = stage.masterlabel;
        o.Pricebook2Id = pb.id;

        /* Create the opportunity */
        Database.insert(o);

        /* Setup a basic Template*/
        PDF_Template__c pd  = new PDF_Template__c();
        pd.Name         = 'English';
        pd.Template_Address__c = 'apex/English';
        
        /* Create the Template */
        Database.insert(pd);        

        /* Setup a basic quote*/
        Quote q  = new Quote();
        q.Name         = 'TEST';
        q.OpportunityId    = o.id;
        q.PDF_Template__c = pd.id;


        /* Create the quote*/
        Database.insert(q);

        /* Create a contact */
        Contact c   = new Contact();
        c.lastname  = 'LASTNAME';
        c.firstname = 'FIRSTNAME';

        Database.insert(c);

        /* Create the opportunity contact role */
        OpportunityContactRole r = new OpportunityContactRole();
        r.ContactId     = c.id;
        r.OpportunityId = o.id;
        r.IsPrimary     = true;
        r.role          = 'ROLE';

        Database.insert(r);

        /* Create a product2 */
        Product2 p = new Product2();
        p.Name     = 'TEST';

        Database.insert(p);

        /* Create a pricebook entry. */
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = pb.id;
        pbe.Product2Id   = p.id;
        pbe.IsActive     = true;
        pbe.UnitPrice    = 1;

        Database.insert(pbe);

        /* Create a line item */
        OpportunityLineItem i = new OpportunityLineItem();
        i.opportunityId       = o.id;
        i.pricebookentryid    = pbe.id;
        i.quantity            = 1;
        i.unitprice           = 1;

        Database.insert(i);

        /* Set up the opportunity with the related records */
        r.Contact        = c;
        r.Opportunity    = o;
        o.Account        = a;
        i.Opportunity    = o;
        pbe.Product2     = p;
        pbe.Pricebook2   = pb;
        i.PricebookEntry = pbe;

        return o;
        PageReference pref = Page.NewQuoteOverride;
        pref.getParameters().put('o.id',o.id);
        Test.setCurrentPage(pref);

        return o;
        
}
}

 

Here is the regular class:

 

public class quoteExtend {

    ApexPages.StandardController controller;
    
        public opportunity o {get;set;}
        public quote q {get;set;}
        public ID oid {get; set;}
        Id u = UserInfo.getUserId();
        string bv = 'Key BV English';
        string ww = 'Key WW English';
    public quoteExtend (ApexPages.StandardController c) {
        controller = c;

        q = (quote) c.getRecord();
        system.debug('SFDC TEST *******'+q);
        oid = System.currentpageReference().getParameters().get('oppid');
        String Typ = System.currentpageReference().getParameters().get('Type');
        String Rec = System.currentpageReference().getParameters().get('Rec');
        
        id brecordtypeid = [select id,name from RecordType where sobjecttype='Quote' and name='Budget Quote'].id;
        Integer bi = 1+ [select count() from Quote where opportunityid = :oid and RecordTypeId=:brecordtypeid ];
        
        id frecordtypeid = [select id,name from RecordType where sobjecttype='Quote' and name='Firm Quote'].id;
        Integer fi = 1+ [select count() from Quote where opportunityid = :oid and RecordTypeId=:frecordtypeid ];
       
       List<OpportunityContactRole> ocr = [select id, opportunityid, isprimary, ContactId,
           Contact.Name, Contact.Phone, Contact.Fax, Contact.Email from
           OpportunityContactRole where opportunityid = :oid and isprimary = true];

        if(oid != null) {
            o = [select id, Account.Name, Account.BillingStreet, Account.BillingCity, Account.BillingState,
                Account.BillingCountry, Account.BillingPostalCode, Account.ShippingStreet, Account.ShippingCity,
                Account.ShippingState, Account.ShippingCountry, Account.ShippingPostalCode, SFDC_For_Quote__c,
                Equip_Services_Options_to_be_quoted__c, Opportunity_Region__c,Pricebook2Id from opportunity where id = :oid];    
 
       string title;
           If (rec == 'Budget Quote') {
          
               title = 'B-' + o.SFDC_for_quote__c + '-' + bi;
          
           } else if (Rec =='Firm Quote')  {
           
               title = 'F-' + o.SFDC_for_quote__c + '-' + fi;
               
           }
          
        q.Opportunityid = o.Id;
        q.name = title;
        If (rec == 'Budget Quote') {
          
               q.Recordtypeid = brecordtypeid;
          
           } else if (Rec =='Firm Quote')  {
           
               q.Recordtypeid = frecordtypeid;
               
           }
        If (o.Opportunity_Region__c == 'Europe') {
          
               q.PDF_Template_Name__c = bv;
          
           } else   {
           
               q.PDF_Template_Name__c = ww;
               
           }           
        q.BillingName = o.Account.Name;
        q.BillingStreet = o.Account.BillingStreet;
        q.BillingCity = o.Account.BillingCity;
        q.BillingState = o.Account.BillingState;
        q.BillingPostalCode = o.Account.BillingPostalCode;
        q.BillingCountry = o.Account.BillingCountry;
        q.ShippingName = o.Account.Name;
        q.ShippingStreet = o.Account.ShippingStreet;
        q.ShippingCity = o.Account.ShippingCity;
        q.ShippingState = o.Account.ShippingState;
        q.ShippingPostalCode = o.Account.ShippingPostalCode;
        q.ShippingCountry = o.Account.ShippingCountry;
        q.Contactid = ocr[0].contactid;
        q.Email = ocr[0].contact.email;
        q.Phone = ocr[0].contact.Phone;
        q.Fax = ocr[0].contact.Fax;
        q.Pricebook2Id = o.Pricebook2Id;
        q.Project_Scope__c = o.Equip_Services_Options_to_be_quoted__c;
  
     }
     }

    public PageReference save() {

        PageReference p = controller.save();
        id oid = controller.getId();
        
          return p;
    }

}

 

Thank you,

ckellie

Best Answer chosen by Admin (Salesforce Developers) 
WizradWizrad

Your understanding is correct!

 

Your code says:

 

oid = System.currentpageReference().getParameters().get('oppid');

 

And you need to get control flow to enter the following if statement

 

if(oid != null)

 Then your unit test can create an opportunity.

 

private static testMethod void myUnitTest() {
    Opportunity testOpp = new Opportunity(Name='TestOppty');
    insert testOpp;

    ApexPages.StandardController sc = new ApexPages.StandardController(testOpp);
    Test.setCurrentPage('/apex/TheNameOfMyVFPage?oppid=' + testOpp.Id);
    quoteExtend qe = new quoteExtend(sc);

    //...Remainder of test code here
}

 

 

All Answers

Shashikant SharmaShashikant Sharma

Could you please share which part you are not able to cover? 

Ankit AroraAnkit Arora

Yes, please share the uncovered code as it is difficult for us to find out because you have some specific field names which doesn't exists on our org.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

ckellieckellie

Here is the uncovered code:

 

            o = [select id, Account.Name, Account.BillingStreet, Account.BillingCity, Account.BillingState,
                Account.BillingCountry, Account.BillingPostalCode, Account.ShippingStreet, Account.ShippingCity,
                Account.ShippingState, Account.ShippingCountry, Account.ShippingPostalCode, SFDC_For_Quote__c,
                Equip_Services_Options_to_be_quoted__c, Opportunity_Region__c,Pricebook2Id from opportunity where id = :oid];    
 
       string title;
           If (rec == 'Budget Quote') {
          
               title = 'B-' + o.SFDC_for_quote__c + '-' + bi;
          
           } else if (Rec =='Firm Quote')  {
           
               title = 'F-' + o.SFDC_for_quote__c + '-' + fi;
               
           }
          
        q.Opportunityid = o.Id;
        q.name = title;
        If (rec == 'Budget Quote') {
          
               q.Recordtypeid = brecordtypeid;
          
           } else if (Rec =='Firm Quote')  {
           
               q.Recordtypeid = frecordtypeid;
               
           }
        If (o.Opportunity_Region__c == 'Europe') {
          
               q.PDF_Template_Name__c = bv;
          
           } else   {
           
               q.PDF_Template_Name__c = ww;
               
           }           
        q.BillingName = o.Account.Name;
        q.BillingStreet = o.Account.BillingStreet;
        q.BillingCity = o.Account.BillingCity;
        q.BillingState = o.Account.BillingState;
        q.BillingPostalCode = o.Account.BillingPostalCode;
        q.BillingCountry = o.Account.BillingCountry;
        q.ShippingName = o.Account.Name;
        q.ShippingStreet = o.Account.ShippingStreet;
        q.ShippingCity = o.Account.ShippingCity;
        q.ShippingState = o.Account.ShippingState;
        q.ShippingPostalCode = o.Account.ShippingPostalCode;
        q.ShippingCountry = o.Account.ShippingCountry;
        q.Contactid = ocr[0].contactid;
        q.Email = ocr[0].contact.email;
        q.Phone = ocr[0].contact.Phone;
        q.Fax = ocr[0].contact.Fax;
        q.Pricebook2Id = o.Pricebook2Id;
        q.Project_Scope__c = o.Equip_Services_Options_to_be_quoted__c;
  
     }
     }

 Thank you

WizradWizrad

Just think.

 

"How do I get the control flow to enter the IF statement?"

ckellieckellie

That is where I am coming up blank, how to control  the flow to enter the if statement. I am thinking that I will need to structure my test data to meet the if statement criteria. Does this mean that I need to use the same variables as in the actual class? Do I need to write some if statements in the test code?

 

Thanks,

ckellie

WizradWizrad

Your understanding is correct!

 

Your code says:

 

oid = System.currentpageReference().getParameters().get('oppid');

 

And you need to get control flow to enter the following if statement

 

if(oid != null)

 Then your unit test can create an opportunity.

 

private static testMethod void myUnitTest() {
    Opportunity testOpp = new Opportunity(Name='TestOppty');
    insert testOpp;

    ApexPages.StandardController sc = new ApexPages.StandardController(testOpp);
    Test.setCurrentPage('/apex/TheNameOfMyVFPage?oppid=' + testOpp.Id);
    quoteExtend qe = new quoteExtend(sc);

    //...Remainder of test code here
}

 

 

This was selected as the best answer
ckellieckellie

Thank you for the education and help.