-2

I am not able to increase the coverage of this class more than 45%.Can anyone suggest me how to increase the coverage.

Apex Class:-

Public class ProductSearchPageController{
    public Product2 prodFilter                          { get; set; }
    //public list<String> selectedStatus                { get; set; }
    public List<Material_Profile__c> CustomerAccList    { get; set; }
    public List<Material_Profile__c> SupplierAccList    { get; set; }
    public string SearchProductName                     { get; set; }
    public List<string> statusList                      { get; set; }
    public List<string> additive1List                   { get; set; }
    public List<string> additive2List                   { get; set; }
    public List<string> additive3List                   { get; set; }
    public List<string> additive4List                   { get; set; }
    public List<string> additive5List                   { get; set; }
    public List<string> ColorList                       { get; set; }
    public List<string> productGroupList                { get; set; }
    public List<string> productSubCatList               { get; set; }
    public List<string> polymerTypeList                 { get; set; }
    public List<string> certificationList               { get; set; }

    public List<string> selectedstatusList                      { get; set; }
    public List<string> selectedadditive1List                   { get; set; }
    public List<string> selectedadditive2List                   { get; set; }
    public List<string> selectedadditive3List                   { get; set; }
    public List<string> selectedadditive4List                   { get; set; }
    public List<string> selectedadditive5List                   { get; set; }
    public List<string> selectedColorList                       { get; set; }
    public List<string> selectedproductGroupList                { get; set; }
    public List<string> selectedproductSubCatList               { get; set; }
    public List<string> selectedpolymerTypeList                 { get; set; }
    public List<string> selectedcertificationList               { get; set; }     

    public string multiselectedValue                    { get; set; }  
    public string BillingCity                           { get; set; }
    public string BillingState                          { get; set; }
    public string BillingCountry                        { get; set; }    


    Public ProductSearchPageController(){
       // selectedStatus = new list<String>();
        prodFilter = new Product2(); 
        BillingCity = '';
        BillingState = '';
        BillingCountry = '';
        selectedstatusList  = new List<String>();
        selectedadditive1List  = new List<String>();
        selectedadditive2List  = new List<String>();
        selectedadditive3List  = new List<String>();
        selectedadditive4List  = new List<String>();
        selectedadditive5List  = new List<String>();
        selectedColorList  = new List<String>();
        selectedproductGroupList  = new List<String>();
        selectedproductSubCatList  = new List<String>();
        selectedpolymerTypeList  = new List<String>();
        selectedcertificationList  = new List<String>();
        multiselectedValue='';

       //---------------------status field------ 
         statusList = getPicklistValues('Product2','Status__c');

      //---------------------Additive1 field------          
         additive1List = getPicklistValues('Product2','Product_Type_Additive_1__c');

        //---------------------Additive2 field------          
         additive2List = getPicklistValues('Product2','Product_Type_Additive_2__c');

        //---------------------Additive3 field------          
         additive3List = getPicklistValues('Product2','Product_Type_Additive_3__c');        

        //---------------------Additive4 field------          
         additive4List = getPicklistValues('Product2','Product_Type_Additive_4__c');

        //---------------------Additive5 field------          
         additive5List = getPicklistValues('Product2','Product_Type_Additive_5__c');

        //---------------------Color field------          
        ColorList = getPicklistValues('Product2','color__c'); 

       //---------------------Product Group field------          
         productGroupList = getPicklistValues('Product2','Product_Group__c');

       //---------------------Product Sub Category field------          
         productSubCatList = getPicklistValues('Product2','Product_Sub_Category__c');

       //---------------------Polymer Type field------          
          polymerTypeList = getPicklistValues('Product2','Polymer_Type__c');

       //---------------------Certification field------          
         certificationList = getPicklistValues('Product2','Certification__c');

        CustomerAccList=new List<Material_Profile__c>();
        SupplierAccList=new List<Material_Profile__c>();
        /*for(Material_Profile__c mp :[SELECT Customer__c,Supplier__c ,Customer__r.Name,Supplier__r.Name ,Product__c, Product__r.name ,Customer__r.RecordType.Name, Supplier__r.RecordType.Name,Customer__r.Phone,Supplier__r.Phone,
                                     Customer__r.Category_of_Customer__c , Customer__r.Type_of_Customer__c ,Supplier__r.Category_of_Supplier__c,Supplier__r.Type_of_Supplier__c
                                     FROM Material_Profile__c
                                     Order By  product__c ,Customer__r.RecordType.Name, Supplier__r.RecordType.Name]){
                                         if(mp.Customer__c!=null){
                                             CustomerAccList.add(mp);
                                         }
                                         if(mp.Supplier__c!=null){
                                             SupplierAccList.add(mp);
                                         }
                                     }*/


    }

      public static List<String> getPicklistValues(String ObjectApi_name,String fieldName){ 
        List<String> lstPickVals=new List<String>();
        Schema.SObjectType targetType = Schema.getGlobalDescribe().get(objectApi_name);
        Sobject ObjName = targetType.newSObject();
        Schema.SObjectType sobjectType = objName.getSObjectType();
        Schema.DescribeSObjectResult sObjectDescribe = sobjectType.getDescribe();

        Map<String,Schema.SObjectField> fieldMap = sObjectDescribe.fields.getMap();
        List<Schema.PicklistEntry> pickListValues = fieldMap.get(fieldName).getDescribe().getPicklistValues();
        for(Schema.PicklistEntry ple : pickListValues)
            lstPickVals.add(ple.getValue());
    return lstPickvals;
    }

    public void seletedValueDisposing(){
        if(multiselectedValue != Null){
            List<string> valueList = multiselectedValue.split(':');

            if(valueList.size()>1){
                if(valueList[0]=='Status'){
                    valueList[1] = valueList[1].removeEnd(';');
                    List<string> ValList = valueList[1].split(';');
                    selectedstatusList = new List<string>();
                    for(integer i=0;i<ValList.size();i++){
                       selectedstatusList.add(ValList[i]);
                    }
                }
                else if(valueList[0]=='productGroup'){
                    valueList[1] = valueList[1].removeEnd(';');
                    List<string> ValList = valueList[1].split(';');
                    selectedproductGroupList = new List<string>();
                    for(integer i=0;i<ValList.size();i++){
                       selectedproductGroupList.add(ValList[i]);
                    }
                }
                else if(valueList[0]=='productSub'){
                    valueList[1] = valueList[1].removeEnd(';');
                    List<string> ValList = valueList[1].split(';');
                    selectedproductSubCatList = new List<string>();
                    for(integer i=0;i<ValList.size();i++){
                       selectedproductSubCatList.add(ValList[i]);
                    }
                }
                else if(valueList[0]=='polymerType'){
                    valueList[1] = valueList[1].removeEnd(';');
                    List<string> ValList = valueList[1].split(';');
                    selectedpolymerTypeList = new List<string>();
                    for(integer i=0;i<ValList.size();i++){
                       selectedpolymerTypeList.add(ValList[i]);
                    }
                }
                else if(valueList[0]=='certification'){
                    valueList[1] = valueList[1].removeEnd(';');
                    List<string> ValList = valueList[1].split(';');
                    selectedcertificationList = new List<string>();
                    for(integer i=0;i<ValList.size();i++){
                       selectedcertificationList.add(ValList[i]);
                    }
                }
                else if(valueList[0]=='additive1'){
                    valueList[1] = valueList[1].removeEnd(';');
                    List<string> ValList = valueList[1].split(';');
                    selectedadditive1List = new List<string>();
                    for(integer i=0;i<ValList.size();i++){
                       selectedadditive1List.add(ValList[i]);
                    }
                }
                else if(valueList[0]=='additive2'){
                    valueList[1] = valueList[1].removeEnd(';');
                    List<string> ValList = valueList[1].split(';');
                    selectedadditive2List = new List<string>();
                    for(integer i=0;i<ValList.size();i++){
                       selectedadditive2List.add(ValList[i]);
                    }
                }
                else if(valueList[0]=='additive3'){
                    valueList[1] = valueList[1].removeEnd(';');
                    List<string> ValList = valueList[1].split(';');
                    selectedadditive3List = new List<string>();
                    for(integer i=0;i<ValList.size();i++){
                       selectedadditive3List.add(ValList[i]);
                    }
                }
                else if(valueList[0]=='additive4'){
                    valueList[1] = valueList[1].removeEnd(';');
                    List<string> ValList = valueList[1].split(';');
                    additive4List = new List<string>();
                    for(integer i=0;i<ValList.size();i++){
                       additive4List.add(ValList[i]);
                    }
                }
                else if(valueList[0]=='additive5'){
                    valueList[1] = valueList[1].removeEnd(';');
                    List<string> ValList = valueList[1].split(';');
                    selectedadditive5List = new List<string>();
                    for(integer i=0;i<ValList.size();i++){
                       selectedadditive5List.add(ValList[i]);
                    }
                }
                else if(valueList[0]=='Color'){
                    valueList[1] = valueList[1].removeEnd(';');
                    List<string> ValList = valueList[1].split(';');
                    selectedColorList = new List<string>();
                    for(integer i=0;i<ValList.size();i++){
                       selectedColorList.add(ValList[i]);
                    }
                }
            }
            else{
                if(valueList[0]=='Status'){
                    selectedstatusList = new List<string>();
                }
                else if(valueList[0]=='productGroup'){
                    selectedproductGroupList = new List<string>();
                }
                else if(valueList[0]=='productSub'){
                    selectedproductSubCatList = new List<string>();
                }
                else if(valueList[0]=='polymerType'){
                    selectedpolymerTypeList = new List<string>();
                }
                else if(valueList[0]=='certification'){
                    selectedcertificationList = new List<string>();
                }
                else if(valueList[0]=='additive1'){
                    selectedadditive1List = new List<string>();
                }
                else if(valueList[0]=='additive2'){
                    selectedadditive2List = new List<string>();
                }
                else if(valueList[0]=='additive3'){
                    selectedadditive3List = new List<string>();
                }
                else if(valueList[0]=='additive4'){
                    additive4List = new List<string>();
                }
                else if(valueList[0]=='additive5'){
                    selectedadditive5List = new List<string>();
                }
                else if(valueList[0]=='Color'){
                    selectedColorList = new List<string>();
                }
            }

            string query = queryStringSetup();
            boolean multiselectFilter = False;
            if(selectedstatusList.size()>0){
                multiselectFilter = True;
                query+= ' product__r.Status__c Includes '+selectedstatusList;
            }

            if(selectedproductGroupList.size()>0){
                if(multiselectFilter)
                    query+=' AND ';
                else
                    multiselectFilter = True;
                query+= ' product__r.Product_Group__c Includes '+selectedproductGroupList;
            }

            if(selectedproductSubCatList.size()>0){
                if(multiselectFilter)
                    query+=' AND ';
                else
                    multiselectFilter = True;
                query+= ' product__r.Product_Sub_Category__c Includes '+selectedproductSubCatList;
            }
            if(selectedpolymerTypeList.size()>0){
                if(multiselectFilter)
                    query+=' AND ';
                else
                    multiselectFilter = True;
                query+= ' product__r.Polymer_Type__c Includes '+selectedpolymerTypeList;
            }
            if(selectedcertificationList.size()>0){
                if(multiselectFilter)
                    query+=' AND ';
                else
                    multiselectFilter = True;
                query+= ' product__r.Certification__c Includes '+selectedcertificationList;
            }
            if(selectedadditive1List.size()>0){
                if(multiselectFilter)
                    query+=' AND ';
                else
                    multiselectFilter = True;
                query+= ' product__r.Product_Type_Additive_1__c Includes '+selectedadditive1List;
            }
            if(selectedadditive2List.size()>0){
                if(multiselectFilter)
                    query+=' AND ';
                else
                    multiselectFilter = True;
                query+= ' product__r.Product_Type_Additive_2__c Includes '+selectedadditive2List;
            }
            if(selectedadditive3List.size()>0){
                if(multiselectFilter)
                    query+=' AND ';
                else
                    multiselectFilter = True;
                query+= ' product__r.Product_Type_Additive_3__c Includes '+selectedadditive3List;
            }
            if(selectedadditive4List.size()>0){
                if(multiselectFilter)
                    query+=' AND ';
                else
                    multiselectFilter = True;
                query+= ' product__r.Product_Type_Additive_4__c Includes '+selectedadditive4List;
            }
            if(selectedadditive5List.size()>0){
                if(multiselectFilter)
                    query+=' AND ';
                else
                    multiselectFilter = True;
                query+= ' product__r.Product_Type_Additive_5__c Includes '+selectedadditive5List;
            }
            if(selectedColorList.size()>0){
                if(multiselectFilter)
                    query+=' AND ';
                else
                    multiselectFilter = True;
                query+= ' product__r.Color__c Includes '+selectedColorList;
            }
            if(prodFilter.USP__c != NULL){
                if(multiselectFilter)
                    query+=' AND ';
                else
                    multiselectFilter = True;
                query+= ' product__r.USP__c LIKE \'%'+prodFilter.USP__c+'%\' ';
            }
            if(prodFilter.Product__c!=NULL){
                if(multiselectFilter)
                    query += ' AND ';
                else
                    multiselectFilter = True;
                query += ' Product__c = \''+prodFilter.Product__c+'\' ';

            }
            if(billingCity != NULL && billingCity != ''){
                if(multiselectFilter)
                    query+=' AND ';
                else
                    multiselectFilter = True;
                query+= ' Customer__r.billingCity LIKE \'%'+billingCity+'%\' ';
            }
            if(billingState != NULL && billingState != ''){
                if(multiselectFilter)
                    query+=' AND ';
                else
                    multiselectFilter = True;
                query+= ' Customer__r.billingState LIKE \'%'+billingState+'%\' ';
            }

            if(billingCountry != NULL && billingCountry != ''){
                if(multiselectFilter)
                    query+=' AND ';
                else
                    multiselectFilter = True;
                query+= ' Customer__r.billingCountry LIKE \'%'+billingCountry+'%\' ';

            }



            system.debug('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'+query);
            if(multiselectFilter)
                filterProducts(query);
            else
                filterProducts(NULL);
        }
        multiselectedValue='';
    }

    public void pageCallFilterProducts(){
        filterProducts(NULL);
    }


    //-------------------------------Filter Products-----------------------------------------------
    public void filterProducts( string preSetString){
        string query='';
        Boolean previousFilter = False;
        if(preSetString==NULL){
            query = queryStringSetup();
        }
        else{
            query = preSetString;
            previousFilter = True;
        }


        if(!previousFilter){
            CustomerAccList=new List<Material_Profile__c>();
            SupplierAccList=new List<Material_Profile__c>();
        }
        else{
            CustomerAccList=new List<Material_Profile__c>();
            SupplierAccList=new List<Material_Profile__c>();
            system.debug('>>>>>>>>>>>>>>>>>>>>&&&&&&&&&<<<<<<<<<<<<<<<'+query);
            for(Material_Profile__c mp : Database.query(query)){
                if(mp.Customer__c!=null){
                    CustomerAccList.add(mp);
                }
                if(mp.Supplier__c!=null){
                    SupplierAccList.add(mp);
                }
            }
        }
    } 

    private string queryStringSetup(){
        String query = 'SELECT Quantity__c,Customer__r.Email__c,Supplier__r.Email__c,Customer__c,Supplier__c ,Customer__r.Name,Supplier__r.Name ,Product__c, Product__r.name ,';
        query += 'Customer__r.RecordType.Name, Supplier__r.RecordType.Name,Customer__r.Phone,Supplier__r.Phone,Customer__r.Category_of_Customer__c ,';
        query += 'Customer__r.Type_of_Customer__c ,Supplier__r.Category_of_Supplier__c,Supplier__r.Type_of_Supplier__c FROM Material_Profile__c WHERE ';

        return query;
    }


}

Test Class :-

@istest
public class ProductSearchPageControllerTest {
    public Static Testmethod void Mytest(){
        Account acc = new Account();
        acc.Name='Fanindra';
        Id cuId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('Customer').getRecordTypeId();
        insert acc;
        product2 pr = new product2();
        pr.Name='Nylon';
        pr.Status__c='Active';
        pr.Customer__c=acc.Id;
        pr.Product_Sub_Category__c='Compounded';
        pr.Product_Type_Additive_1__c='Glass Filled 5%';
        pr.Product_Type_Additive_2__c='Glass Filled 10%';
        pr.Product_Type_Additive_3__c='Product Type Addictive 3';   
        pr.Product_Type_Additive_4__c='Product Type Additive 4';
        pr.Product_Type_Additive_5__c='Product Type Additive 5'; 
        pr.Color__c='Black';
        pr.Product_Group__c='Acetate Butyrate';
        pr.Polymer_Type__c='ABS ';
        pr.Certification__c='UL Certified';
        insert pr;
        Material_Profile__c mp = new Material_Profile__c();
        mp.name='material1';
        mp.Customer__c=acc.Id;
        mp.Product__c=pr.Id;
        insert mp;

        ProductSearchPageController obj=new ProductSearchPageController();
        obj.SupplierAccList.add(mp);

        obj.seletedValueDisposing();
        obj.pageCallFilterProducts();
        obj.SearchProductName='nylon6';

    }
    public Static Testmethod void Mytest1(){
        Account acc = new Account();
        acc.Name='Fanindra';
        Id cuId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('Supplier').getRecordTypeId();
        insert acc;
        product2 pr = new product2();
        pr.Name='Nylon';
        pr.Status__c='Active';
        pr.Customer__c=acc.Id;
        pr.Product_Sub_Category__c='Compounded';
        pr.Product_Type_Additive_1__c='Glass Filled 5%';
        pr.Product_Type_Additive_2__c='Glass Filled 10%';
        pr.Product_Type_Additive_3__c='Product Type Addictive 3';   
        pr.Product_Type_Additive_4__c='Product Type Additive 4';
        pr.Product_Type_Additive_5__c='Product Type Additive 5'; 
        pr.Color__c='Black';
        pr.Product_Group__c='Acetate Butyrate;Acetate Proprionate';
        pr.Polymer_Type__c='ABS ';
        pr.Certification__c='UL Certified';
        insert pr;

        Material_Profile__c mp = new Material_Profile__c();
        mp.name='material1';
        mp.Customer__c=acc.Id;
        mp.Product__c=pr.Id;
        insert mp;

        PageReference pagref = Page.ProductSearchPage   ;
        ProductSearchPageController obj=new ProductSearchPageController();
        obj.seletedValueDisposing();
        obj.pageCallFilterProducts();
        obj.SearchProductName='nylon6';


    }

}

1 Answer 1

0

Your apex class seems multiple if else condition, so you might need to write multiple test method to cover the different condition.

Here I provided some tips to write test class, it will help you to improved the code coverage.

I've provided example so you will get idea how it will work.

Example :

Sample Apex class

public class apexclass{

    public function1(){

        if(condition)
        {
            logic1
        }
        else{
            logic2
        }
    }
    public function2(){

        logic3
    }

}

Test class to cover 100% code coverage for above class:

@istest
public class ProductSearchPageControllerTest {
    public Static testMethod void test1(){
        //Wrtie test class functionlaity
        //try to cover maximum code coverage
        //suppose it cover logic1 of function()1 only. 

        test.startTest();   //test.starttest will reset the governer limit for exectuion of further code till test.stoptset()
        //call your apex class method. 
        test.stopTest();
    }

    //Create another method to cover remaining functionlaity 
    public Static testMethod void test2(){
        //try to cover maximum code coverage
        // example write test functionlaity so that it cover logic2 of function()1. and other methods if possible. 

        test.startTest();   
        //call your apex class method. 
        test.stopTest();
    }

    //Create another method to cover remaining functionlaity 
    public Static testMethod void test3(){
        //try to cover maximum code coverage
        // example write test functionlaity so that it cover logic3 of function()2. 

        test.startTest();   
        //call your apex class method. 
        test.stopTest();
    }
}

If you used the multiple test method in test class so it might be hit the apex governor limit. So best practice to use test.startTest() and test.stopTest(). As provided in example. For detail refer this


If you have to use common data across all method, use testsetup method. So it can avoid multiple DML operation to perform in every method.

Use test setup methods (methods that are annotated with @testSetup) to create test records once and then access them in every test method in the test class. Test setup methods can reduce test execution times especially when you’re working with many records.

Test setup methods are defined in a test class, take no arguments, and return no value

 @testSetup static void methodName() {

}

For more detail refer this

You must log in to answer this question.

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