-1

I have a wrapper class that I can't seem to get more than 20% coverage on, can someone help? Here is the controller code:

public class Add_QLI {
public String opportunitystringId {get;set;}
public List<Opportunity> opportunityList {get;set;}
public List<quotewrapper> quotewrapperlist {get;set;}
public List<OpportunityLineItem> oliList{get;set;}
public Opportunity CurrentOpp{get;set;}
public Opportunity SourceOpp{get;set;}

public class quotewrapper
{
    public Boolean isChecked {get;set;}
    public OpportunityLineItem oliresult {get;set;}
    
    public quotewrapper(Boolean isChecked, OpportunityLineItem oliresult)
    {
        This.isChecked = isChecked;
        This.oliresult = oliresult;
    } 
}   

public Add_QLI(ApexPages.StandardController controller)
{CurrentOpp = (Opportunity)controller.getRecord();
 SourceOpp = [SELECT id,Percentage_Uplift__c FROM Opportunity WHERE id = :CurrentOpp.id];
    try{
        quotewrapperlist = new List<quotewrapper>();
        opportunitystringId  = ApexPages.CurrentPage().getparameters().get('Id');
        
        if(opportunitystringId!=null)
        {
            opportunityList = [SELECT Id,Name,CloseDate,AccountId,Pricebook2Id from Opportunity WHERE Id =:opportunitystringId];
            oliList = [Select Id,Name,Quantity,OpportunityId,UnitPrice,Product2Id,PricebookentryId,TotalPrice,Height_M__c,Length_M__c,Hire_in_Weeks__c,Extra_weeks_after_initial_20_weeks__c from OpportunityLineItem WHERE OpportunityId =:opportunitystringId order by Name];
        }
        
        if(oliList.size()>0)
        {
            for(OpportunityLineItem olObj:oliList)
            {
                quotewrapper qobj =  new quotewrapper(false, olObj);
                quotewrapperlist.add(qobj);
            }
        }
    }
    catch(Exception e)
    {
        System.debug('The following exception has occurred: ' + e.getMessage());
    }       
}

public pagereference saveQuote()
{
    try{
        List<OpportunityLineItem> olilistNew = new List<OpportunityLineItem>();  
        List<QuoteLineItem>  quoteItemList = new List<QuoteLineItem>();
        
        for(quotewrapper qwr : quotewrapperlist)
        {
            if(qwr.isChecked==true)
            {
                olilistNew.add(qwr.oliresult);
            }
        }
        
        List<Quote> quoteListNew = new List<Quote>();
        if(opportunityList.size()>0)
        {
            for(Opportunity opportunityObj : opportunityList)
            {
                Quote quoteObj = new Quote();
                quoteObj.Name=datetime.now()+opportunityObj.Name;
                quoteObj.OpportunityId = opportunityObj.Id;
                quoteObj.Pricebook2Id =opportunityObj.Pricebook2Id;
                quoteObj.Percentage_Uplift__c =SourceOpp.Percentage_Uplift__c;
                quoteListNew.add(quoteObj); 
            }
        }
        if(quoteListNew.size()>0)
        {
            Database.SaveResult[] quoteItemSaveList = Database.insert(quoteListNew);
        }
        if(olilistNew.size()>0)
        {
            for(Quote qutObj: quoteListNew)
            { 
                for(OpportunityLineItem oliObj : olilistNew)
                {  
                    QuoteLineItem qlobj = new QuoteLineItem();
                    qlobj.Quantity=oliObj.Quantity;
                    qlobj.PricebookEntryId=oliObj.PricebookEntryId;
                    qlobj.QuoteId=qutObj.Id;
                    qlobj.Product2Id=oliObj.Product2Id;
                    qlobj.UnitPrice=oliObj.UnitPrice;
                    qlobj.Height_M__c=oliObj.Height_M__c;
                    qlobj.Length_M__c=oliObj.Length_M__c;
                    qlobj.Hire_in_Weeks__c=oliObj.Hire_in_Weeks__c;
                    qlobj.Extra_weeks_after_initial_20_weeks__c=oliObj.Extra_weeks_after_initial_20_weeks__c;
                    quoteItemList.add(qlobj);   
                }
            }
            if(quoteItemList.size()>0)
            {
                Database.SaveResult[] quoteItemSaveList = Database.insert(quoteItemList);
            }
        } 
    } 
    
    catch(Exception e)
    {
        System.debug('The following exception has occurred: ' + e.getMessage());
    }
    PageReference pageWhereWeWantToGo = new ApexPages.StandardController(CurrentOpp).view(); //we want to redirect the User back to the Account detail page
    pageWhereWeWantToGo.setRedirect(true); //indicate that the redirect should be performed on the client side
    return pageWhereWeWantToGo; //send the User on their way
}
}

I have a test class that covers 20% but I can't seem to get more than that:

private class Add_QLITest {
public static testmethod void testCurrentOpp(){

    Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];

    List<Product2> prodtoinsert = new list<Product2>{
    new Product2 (Name='test1',Family='Liner', IsActive=true),
    new Product2 (Name='test2',Family='Liner', IsActive=true)    
        };
    insert prodtoinsert;

    PricebookEntry pbe = new PricebookEntry (Product2Id=prodtoinsert[0].id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
    insert pbe;
    PricebookEntry pbe2 = new PricebookEntry (Product2Id=prodtoinsert[1].id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
    insert pbe2;
    
    Opportunity o = new Opportunity(Pricebook2Id=standardPb.id,Name='Testing Opportunity', Percentage_Uplift__c=0.10,StageName='New', CloseDate=system.today());
    insert o;
    
    List<OpportunityLineItem> oliList = new List <OpportunityLineItem>{
        new OpportunityLineItem (Product2Id=prodtoinsert[0].id,Quantity=1,OpportunityId=o.Id,UnitPrice =pbe.UnitPrice),
        new OpportunityLineItem (Product2Id=prodtoinsert[1].id,Quantity=1,OpportunityId=o.Id,UnitPrice =pbe.UnitPrice)    
            };
   insert oliList;
    
   Quote q = new Quote(Name=datetime.now()+o.Name, Pricebook2Id = standardPb.id, OpportunityId=o.id,Percentage_Uplift__c=o.Percentage_Uplift__c);
    insert q; 
    
    Apexpages.StandardController sc = new Apexpages.StandardController(o);
    Add_QLI ext = new Add_QLI(sc);

    }
}
3
  • Can you please share your test class coverage as well?
    – Mahmood
    Oct 19, 2022 at 12:42
  • Hi Mahmood - the test class is the second element in the post - is this what you meant? I've just realised it's missing the top line which is @isTest. The test class doesn't seem to cover the pagereference for savequote() or the quotewrapper class Oct 19, 2022 at 13:25
  • Sorry I understand - here is a link to a document with code coverage in it - docs.google.com/document/d/1XUwSYoaMd8MyGi9KJ8QyOHQ2x4EUuR6t/… Oct 19, 2022 at 13:49

1 Answer 1

1

Please note that testmethod keyword is deprecated and you need to use @isTest annotation now for both class and test methods.

The testMethod keyword is now deprecated. Use the @isTest annotation on classes and methods instead. The @isTest annotation on methods is equivalent to the testMethod keyword.


@isTest vs testmethod

Some benefits of using @isTest over testmethod are

  • Code isolation - you can enable/disable testmethods declared with @isTest just by commenting the @isTest and it won't run as part of the testing framework. e.g.

     // @isTest 
     static void testSomethingCool(){
    
     }
    
  • SeeAllData=true - you can set SeeAllData for specific method only if that is not set at the class-level.

     @isTest(SeeAllData=true) 
         static void testWithSeeAllData(){
     }
    

So, your new test class should look something like

@isTest
class Add_QLITest {
    
    @isTest 
    static void testCurrentOpp(){
         // test data
         // unit tests
    }
}

Now, once I checked your code, it was not covered from this point onwards:

opportunitystringId  = ApexPages.CurrentPage().getparameters().get('Id');
            
if(opportunitystringId!=null)

As, the opportunitystringId is being set from the page parameter but you didn't set the page parameter in test class, hence the coverage issue. You can overcome this by

PageReference pageRef =  new ApexPages.StandardController(o).view(); // page reference to opportunity standard view
Test.setCurrentPage(pageRef);
pageRef.getParameters().put('Id', String.valueOf(o.Id));    // insert opportunity id as page parameter
        
Apexpages.StandardController sc = new Apexpages.StandardController(o);
Add_QLI ext = new Add_QLI(sc);
ext.saveQuote();            // call the saveQuote method of class, this is to mimic the controller's action of saving data.

Few Notes on Test class structure of yours

  1. Consider using @testSetup or testdatafactory to setup test data.
  2. Unit tests are missing (your focus should not be on covering the code but to test each use-case of your class) - please review the post - how to write unit tests for Apex
2
  • This is fantastic - thank you, just one more think I'm hoping you can help with - I need to set the ischecked parameter to true for the full coverage to create the record, but I can't seem to pass that variable into a new list - I keep hitting contructor not defined: ext.quotewrapperlist = new List<Add_QLI.quotewrapper>(); Add_QLI.quotewrapper newlinelist = new Add_QLI.quotewrapper(); newlinelist.isChecked = true; ext.quotewrapperlist.add(newlinelist); Oct 20, 2022 at 11:45
  • 1
    all good, thank you so much again for your help I managed to code this myself: for(Add_QLI.quotewrapper QLICheck : ext.quotewrapperlist){ QLICheck.isChecked=true; } Oct 20, 2022 at 12:35

You must log in to answer this question.

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