0

This question already has an answer here:

I am Unable to cover the code. I have inserted all the possible data in test class still the code coverage is 35%. Can someone please help?

Apex class :

    global class ProductCustomController
{

    public static String accountId{get; set;}
    public static List<Opportunity> lstopportunity{get;set;}
    public Map<String,Map<String,List<Opportunity>>> mapFamilyPRodValues{get;set;}
    public Map<String,List<Opportunity> >mapCategorizedPRoValues{get;set;}

    public ProductCustomController(ApexPages.StandardController controller){
        accountId= Controller.getRecord().id;
    }

    @RemoteAction
    global static Map<String,Map<String,Map<String,Set<Opportunity>>>> getItems(String accId) { 
        String accountId = accId;
        lstopportunity= [Select (Select Id,Quantity1__c,Monthly_GP__c,Name,Installed__c,Cost_Price_Per_Month__c,Unit_Price_Per_Month__c,Product2Id,
        Product2.Name,Product2.Product_Family__c,Product2.Product_Category1__c,Total_One_Off_GP__c from OpportunityLineItems where Status__c IN ('Processing', 'Active (orange)','Installing','Active (green)', 'Cancelling') ORDER BY Product2.Product_Family__c,Product2.Product_Category1__c),Id,Name,Minimum_term__c,StageName from Opportunity where AccountId =:accountId];
        Map<Set<String>,Map<String,Map<String,sObject>>> DataMap = new Map<Set<String>,Map<String,Map<String,sObject>>>();
        Set<String> setOfCategory = new Set<String>();
        Map<String,Map<String,Map<String,Set<Opportunity>>>> mapOfCategory = new Map<String,Map<String,Map<String,Set<Opportunity>>>>();

        for(Opportunity ListOfOpp : lstopportunity){
           for(OpportunityLineItem lineitem: ListOfOpp.OpportunityLineItems){ 
            if(mapOfCategory.containsKey(lineitem.Product2.Product_Category1__c)){

                Map<String,Map<String,Set<Opportunity>>> FamilyProductValues = mapOfCategory.get(lineitem.Product2.Product_Category1__c);
                if(FamilyProductValues.containsKey(lineitem.Product2.Product_Family__c)){

                    Map<String,Set<Opportunity>> ProductOppvalue = FamilyProductValues.get(lineitem.Product2.Product_Family__c);
                    if(ProductOppvalue.containsKey(lineitem.Product2.Name)){

                        Set<Opportunity> oppValue = ProductOppvalue.get(lineitem.Product2.Name);
                        oppValue.add(ListOfOpp);
                        ProductOppvalue.put(lineitem.Product2.Name,oppValue);
                    }
                    else{
                        Set<Opportunity> NewListOfOpp = new Set<Opportunity>();
                        NewListOfOpp.add(ListOfOpp);
                        ProductOppvalue.put(lineitem.Product2.Name,NewListOfOpp);
                    }
                    FamilyProductValues.put(lineitem.Product2.Product_Family__c,ProductOppvalue);
                }
                else{
                    Map<String,Set<Opportunity>> NewProductOppvalue = new Map<String,Set<Opportunity>>();
                    Set<Opportunity> NewListOfOpp = new Set<Opportunity>();
                    NewListOfOpp.add(ListOfOpp);
                    NewProductOppvalue.put(lineitem.Product2.Name,NewListOfOpp);

                    FamilyProductValues.put(lineitem.Product2.Product_Family__c,NewProductOppvalue);
                }
            }               
            else{
                Map<String,Map<String,Set<Opportunity>>> NewFamilyProductValues = new Map<String,Map<String,Set<Opportunity>>>();
                Map<String,Set<Opportunity>> NewProductOppvalue = new Map<String,Set<Opportunity>>();
                Set<Opportunity> NewListOfOpp = new Set<Opportunity>();
                NewListOfOpp.add(ListOfOpp);
                NewProductOppvalue.put(lineitem.Product2.Name,NewListOfOpp);
                NewFamilyProductValues.put(lineitem.Product2.Product_Family__c,NewProductOppvalue);
                mapOfCategory.put(lineitem.Product2.Product_Category1__c,NewFamilyProductValues);
            }
           }
        }
        return mapOfCategory;
    }
}

Test class :

    @isTest
public class ProductCustomControllerTest{
     public Map<String,Map<String,List<Opportunity>>> mapFamilyPRodValues{get;set;}
    public Map<String,List<Opportunity> >mapCategorizedPRoValues{get;set;}
    public static testMethod void testProducts1() {
        Test.Starttest();

        Account acc = new Account();
        acc.Name = 'test';
        acc.Phone = '78676565656';
        acc.Type = 'Customer';
        acc.Industry= 'Agriculture & Mining';
        acc.NumberofUKSites__c = '1';
        acc.Employees_DueDil__c = '10-19';
        acc.SIC_Code_DueDil__c = 'test';
        insert acc; 
        System.debug('Debug Log : acc Insertion Successful');
ApexPages.StandardController stdController = new ApexPages.StandardController(acc);
        ProductCustomController con = new ProductCustomController(stdController);        


        Opportunity opp = new Opportunity(Name='Test', 
                                              Account = acc, 
                                              StageName='Qualify',
                                              Company_reg_no__c ='test',
                                              Type = 'Connectivity',
                                              Minimum_term__c = 6546,
                                              CompellingEvent__c = 'test',
                                              NextStep  = 'test',
                                              CloseDate = System.today(),
                                              Delivery_Address_same_as_Installation_ad__c = true,
                                              Installation_address_same_as_Billing_Add__c = true,
                                              //Product2 = prod,
                                              //City__c='test', 

                                              AccountId=acc.Id, 

                                              Probability=100);

        insert opp; 

        Id pricebookId = Test.getStandardPricebookId();

            //Create your product
            Product2 prod = new Product2(
                Product_Category1__c='Mobile' ,Name = 'Laptop X200', Sale_Price_One_Off__c = 03.00,Sale_Price_Per_Month__c = 03.00,  Cost_Price_One_Off__c = 02.00, Cost_Price_Per_Month__c = 02.00
            );
            insert prod;

            //Create your pricebook entry
            PricebookEntry pbEntry = new PricebookEntry(
                 Pricebook2Id = pricebookId,
                 Product2Id = prod.Id,
                 UnitPrice = 100.00,
                 IsActive = true
            );
            insert pbEntry;

            //create your opportunity line item.  This assumes you already have an opportunity created, called opp
            OpportunityLineItem oli = new OpportunityLineItem(
                 OpportunityId = opp.Id,
                 Quantity = 5,
                 PricebookEntryId = pbEntry.Id,
                 TotalPrice = 3 * pbEntry.UnitPrice
            );
            insert oli;
             OpportunityLineItem oli1 = new OpportunityLineItem(
                 OpportunityId = opp.Id,
                 Quantity = 5,
                 PricebookEntryId = pbEntry.Id,
                 TotalPrice = 3 * pbEntry.UnitPrice
            );
            insert oli1;

        String accid = acc.id;
        ProductCustomController.getItems(accid);
       test.stopTest();
    }
}

marked as duplicate by Vijay Ganji, David Reed apex Jun 28 at 12:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Maximizing coverage means you need to exercise the code to ensure the tests execute as many lines of code as possible. In your case, that means you need enough data and appropriate combinations to cause every if and else to be processed.

You should look at the specific conditions for every possible branch in your code and ensure you have some test data that matches the criteria (these must be both the "positive match" of an if expression AND the "negative match" so your else blocks are executed too).

Not the answer you're looking for? Browse other questions tagged or ask your own question.