function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Daniel MasonDaniel Mason 

73% Code Coverage and struggling to find ways to increase the code coverage

Afternoon Team, 
I am hoping you can help me.
I have the created the following class & test class (see below) however i am getting Code Coverage 73% (45/61). Using the developer console i can see that i am not testing the following scenarios on these lines 72-76,82-84,95,98-105. I have been trying all week to increase my code coverage, but i am really struggling to find a solution

if its not to much bother is it possible if you can help me review my test class and get the code coverage above the 75% threshold needed so i can deploy.

Class

/*************************************************************************************************************************
Class Name      :   SalesMarketing
Class Desc.     :   Class used for Sales & Marketing Visual Force Page   
Author          :   DM
Description     :   This class is used to populate the Sales & Marketing VF component, displaying  Material Name,Product, 
                    item from Material_Records__c and Quantity from Materials_Junction__c. Records are selected from the 
                    page (using the checkbox) once an quantity is added the user pushes "save" these records will map 
                    back to the "Sales & Marketing" Object with relevant information selected.
                    
***************************************************************************************************************************/


public with sharing class SalesMarketing
{
    public List<Material_Records__c> Materials {get;set;} 
    public List<materialWrapper> materialWrapperList {get;set;} 
    public Materials_Junction__c marketingJunction {get;set;}
    public String searchString {get;set;} 
    public SalesMarketing(ApexPages.StandardController controller)
    //public SalesMarketing()
    {   
        system.debug('++ Inside Constructor ');
        marketingJunction = (Materials_Junction__c) controller.getRecord();
        materialWrapperList = new List<materialWrapper>();
        Materials = [select ID,name,Product__c, Item__c, Active__c from Material_Records__c where Active__c =true limit 10];
        for(Material_Records__c obj : Materials)
        {
            materialWrapper tempObj= new materialWrapper();
            tempObj.recordId = obj.id;
            tempObj.name = obj.name;
            tempObj.product = obj.Product__c;
            tempObj.item = obj.Item__c;
            tempObj.selectB = false;
            materialWrapperList.add(tempObj);
        }
    }
    
    public Pagereference getProductOrItemData(){
        materialWrapperList.clear();
        system.debug('+++ searchString is '+ searchString); 
        system.debug('+++ Length for searchString is '+ searchString.length());  
        //String searchVal= searchString.trim();
        String searchVal='%'+searchString.trim() +'%';
        system.debug('+++ searchString is '+ searchVal); 

         Materials = [select ID,name,Product__c, Item__c, Active__c from Material_Records__c where Active__c =true and (Product__c like :searchVal OR Item__c like :searchVal)];
        for(Material_Records__c obj : Materials)
        {   
            materialWrapper tempObj= new materialWrapper();
            tempObj.recordId = obj.id;
            tempObj.name = obj.name;
            tempObj.product = obj.Product__c;
            tempObj.item = obj.Item__c;
            tempObj.selectB = false;
            materialWrapperList.add(tempObj);
        }
        system.debug('+++ materialWrapperList is '+ materialWrapperList);
        system.debug('++ materialWrapperList size is '+ materialWrapperList.size());
        return null;
    }

    //save method
    public Pagereference save()
    {
        list<Materials_Junction__c> recordToInsert = new list<Materials_Junction__c>();
        try{
        for(materialWrapper obj : materialWrapperList)
        {
         Materials_Junction__c temp ;
            if(obj.selectB == true)
            {
               temp = new Materials_Junction__c();
               temp.sales_and_marketing__c = marketingJunction.sales_and_marketing__c;
               temp.Material_Records__c= obj.recordId;
               temp.quantity__C = obj.quantity; 
               recordToInsert.add(temp);
            }
            //recordToInsert.add(temp); you are adding element outside the if condition that the reason for save button error
        }
        insert recordToInsert; 
        return new Pagereference('/'+marketingJunction.sales_and_marketing__c);
        }catch(Exception e){
        ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Error,e.getMessage()));
        return null;
        }       
    }
    
    
    public class materialWrapper
    {
        public string recordId {get; set;}
        public string name {get; set;}
        public string product {get; set;}
        public string item {get; set;}
        public Decimal quantity {get; set;}
        public boolean selectB {get; set;}
        
   public void materialWrapper()
       {
           recordId = '';
            name = '';
            product = '';
            item = '';
           quantity = 0.0;
           selectB = false;
       }
    }
}
 

Test Class
 

/*************************************************************************************************************************
Class Name      :   SalesMarketingTest
Author          :   Daniel Mason
Description     :   This test class creates a test record for Sales&Mkt. Also tests VF Page SalesMarketingNew   
Class Used      :   This test class ensure code coverage for NewMKtMaterialsRequest(100%) & SalesMarketing(83%)
***************************************************************************************************************************/   
@isTest
private class SalesMarketingTest
{
    static testmethod void  SalesMarketing()
    {
 
CoreDataTest.getData();


/**This is test data for Creating an Material Request*/          

        sales_and_marketing__c MKTRequest = new sales_and_marketing__c(
               Name = 'Test Name',
               Sales_Contact__c = CoreDataTest.contacts[1].id,
               Number_of_Attendees__c = decimal.valueof('20'),
               Business_Cards__c ='Yes',
                     //  Delivery_Date__c =   datatype "Date/Time" 
               Delivery_Date__c = DateTime.newInstance(2011, 11, 18, 3, 3, 3),
                   //Event_Date__c = datatype "Date" 
               Event_Date__c = Date.newInstance(2017,2,2),
               Delivery_Street__c = 'Test',
               Delivery_City__c  = 'Test',
               Delivery_State_Province__c = 'Test',
               Delivery_Zip_Postal_Code__c = 'Test',
               Delivery_Country__c = 'Test'
                   );
        insert MKTRequest;

/**This is test data for Creating an Material Record*/
        Material_Records__c MRecords= new Material_Records__c(
               Active__c = True,
               Item__c='United States',
               Product__c='Test Name');
        insert MRecords;

/** This is test data for Materials_Junction*/
        Materials_Junction__c MJunction= new Materials_Junction__c(
               Material_Records__c =MRecords.id,
               sales_and_marketing__c= MKTRequest.id);
        insert MJunction;

/**Checking that Materials_Junction__c has a record. */
        MJunction = [
            Select 
            Id, 
            Name, 
            Material_Records__c, 
            sales_and_marketing__c 
                From Materials_Junction__c 
            WHERE sales_and_marketing__r.id =:MKTRequest.Id 
            AND  Material_Records__r.id =:MRecords.id];
            
/**Debug logs*/            
            
system.debug('DAN MKTRequest.Sales_Contact__r.User__c'+MKTRequest.Sales_Contact__r.User__c);
system.debug('DAN MKTRequest.Sales_Contact__c'+MKTRequest.Sales_Contact__c);
system.debug('DAN CoreDataTest.users[0].Id'+CoreDataTest.users[0].Id);
system.debug(' Dan CoreDataTest.users[1].Id '+CoreDataTest.users[1].Id);
system.debug(' Dan UserInfo.getUserId(); '+UserInfo.getUserId());
system.debug(' Dan CoreDataTest.contacts[1].id; '+CoreDataTest.contacts[1].user__C);
            
/**System Assert Statements. Checking that Record has the correct value from insert statements*/

system.assertEquals(MKTRequest.Id, MJunction.sales_and_marketing__c); //Check that Sales& MKT Id is same as MKTRequest.Id
system.assertEquals(MRecords.Id, MJunction.Material_Records__c ); //Check that MaterialRecordsID is same as MRecords.Id

  
// Testing the VFpage: SalesMarketingNew  
ApexPages.StandardController mktnewcontroller= new ApexPages.StandardController(MKTRequest);
NewMKtMaterialsRequest extension = new NewMKtMaterialsRequest(mktnewcontroller); // controller extensionPageReference NewMKT= extension.RedirectToMKTRequest(); 
          

/***Wrapper */
        Test.StartTest();
        ApexPages.StandardController sc = new ApexPages.StandardController(MJunction);

        SalesMarketing controller = new SalesMarketing(sc);
        controller.searchString = 'Test Name';
        controller.getProductOrItemData();
        System.assertEquals(controller.save().getUrl(), '/'+MKTRequest.id);
        SalesMarketing.materialWrapper wrapper= new SalesMarketing.materialWrapper();
        Test.StopTest();
    }
}

 

Amit Chaudhary 8Amit Chaudhary 8
Please try to update your code like below
/*************************************************************************************************************************
Class Name      :   SalesMarketingTest
Author          :   Daniel Mason
Description     :   This test class creates a test record for Sales&Mkt. Also tests VF Page SalesMarketingNew   
Class Used      :   This test class ensure code coverage for NewMKtMaterialsRequest(100%) & SalesMarketing(83%)
***************************************************************************************************************************/   
@isTest
private class SalesMarketingTest
{
    static testmethod void  SalesMarketing()
    {
 
CoreDataTest.getData();


/**This is test data for Creating an Material Request*/          

        sales_and_marketing__c MKTRequest = new sales_and_marketing__c(
               Name = 'Test Name',
               Sales_Contact__c = CoreDataTest.contacts[1].id,
               Number_of_Attendees__c = decimal.valueof('20'),
               Business_Cards__c ='Yes',
                     //  Delivery_Date__c =   datatype "Date/Time" 
               Delivery_Date__c = DateTime.newInstance(2011, 11, 18, 3, 3, 3),
                   //Event_Date__c = datatype "Date" 
               Event_Date__c = Date.newInstance(2017,2,2),
               Delivery_Street__c = 'Test',
               Delivery_City__c  = 'Test',
               Delivery_State_Province__c = 'Test',
               Delivery_Zip_Postal_Code__c = 'Test',
               Delivery_Country__c = 'Test'
                   );
        insert MKTRequest;

/**This is test data for Creating an Material Record*/
        Material_Records__c MRecords= new Material_Records__c(
               Active__c = True,
               Item__c='United States',
               Product__c='Test Name');
        insert MRecords;

/** This is test data for Materials_Junction*/
        Materials_Junction__c MJunction= new Materials_Junction__c(
               Material_Records__c =MRecords.id,
               sales_and_marketing__c= MKTRequest.id);
        insert MJunction;

/**Checking that Materials_Junction__c has a record. */
        MJunction = [
            Select 
            Id, 
            Name, 
            Material_Records__c, 
            sales_and_marketing__c 
                From Materials_Junction__c 
            WHERE sales_and_marketing__r.id =:MKTRequest.Id 
            AND  Material_Records__r.id =:MRecords.id];
            
/**Debug logs*/            
            
system.debug('DAN MKTRequest.Sales_Contact__r.User__c'+MKTRequest.Sales_Contact__r.User__c);
system.debug('DAN MKTRequest.Sales_Contact__c'+MKTRequest.Sales_Contact__c);
system.debug('DAN CoreDataTest.users[0].Id'+CoreDataTest.users[0].Id);
system.debug(' Dan CoreDataTest.users[1].Id '+CoreDataTest.users[1].Id);
system.debug(' Dan UserInfo.getUserId(); '+UserInfo.getUserId());
system.debug(' Dan CoreDataTest.contacts[1].id; '+CoreDataTest.contacts[1].user__C);
            
/**System Assert Statements. Checking that Record has the correct value from insert statements*/

system.assertEquals(MKTRequest.Id, MJunction.sales_and_marketing__c); //Check that Sales& MKT Id is same as MKTRequest.Id
system.assertEquals(MRecords.Id, MJunction.Material_Records__c ); //Check that MaterialRecordsID is same as MRecords.Id

  
// Testing the VFpage: SalesMarketingNew  
ApexPages.StandardController mktnewcontroller= new ApexPages.StandardController(MKTRequest);
NewMKtMaterialsRequest extension = new NewMKtMaterialsRequest(mktnewcontroller); // controller extensionPageReference NewMKT= extension.RedirectToMKTRequest(); 
          

/***Wrapper */
        Test.StartTest();
        ApexPages.StandardController sc = new ApexPages.StandardController(MJunction);

        SalesMarketing controller = new SalesMarketing(sc);
        controller.searchString = 'Test Name';
		
		For( SalesMarketing.materialWrapper mw : controller.materialWrapperList)
		{
			mw.selectB = true;
		}
		
        controller.getProductOrItemData();
		//controller.save();
        System.assertEquals(controller.save().getUrl(), '/'+MKTRequest.id);
        SalesMarketing.materialWrapper wrapper= new SalesMarketing.materialWrapper();
        Test.StopTest();
    }
}

Let us know if this will help you
 
Daniel MasonDaniel Mason

Hi Amit Chaudhary. 

This is the code thats still not being covered. Do you want to screen share? 


User-added imageUser-added image

Daniel MasonDaniel Mason
@Team - is anyone able to help me on my post ?