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
Nikita KapratwarNikita Kapratwar 

Please help me for writing the test Class for my Apex following Class

global class MonthlyMatterInactive 
{
    global static String monthlyMatterInactive() 
    {
        Map<Id,Monthly_Matter_Execution__c> mmeMap = new Map<Id,Monthly_Matter_Execution__c>( [SELECT Id, Date_Executed__c from Monthly_Matter_Execution__c where Date_Executed__c = LAST_MONTH] );
        if(mmeMap.size() != 0)
        {
            list<matter__c> matter= [Select Id, OwnerId, Client__c, Prospect__c, Billing_RateLU__c from Matter__c where Consultation_Matter__c = TRUE and Set_Inactive__c = FALSE Order By CreatedDate DESC Limit 2];
            
            matter__c previousMatter =matter[1];
            
            if(previousMatter != null)
            {
                Matter__c previousMatterUpdate = new Matter__c();
                previousMatterUpdate.Id = previousMatter.Id;
                previousMatterUpdate.Set_Inactive__c = TRUE;
                Update previousMatterUpdate;
            }
        }
        return null;
    }
}



test class with 36% code coverage :

@istest
public class MonthlyMatterInactiveTest
{
    @istest  
    public static void testMethod2() {
        // code_block    
        // 
        test.startTest();
        Account acct = new Account(
            FirstName='test rohini',
            LastName='test twopir'
        ); 
        insert acct;
        
        Propsect__c prop =new Propsect__c(
            Spouses_First_Name__c='test',
            Spouses_Name__c='test',Person__c=acct.id);
        insert prop;
        

        list<matter__c> matter= new list<matter__c>();
        // matter.add(mat);
        
        Matter__c mattercObj = new Matter__c (
            Client__c = acct.id,  
            Prospect__c = prop.id,
            Status__c = 'Active',
            Type__c = 'Consultation',
            Apply_Fixed_Fee__c = FALSE,
            Fixed_fee_Amount__c = NULL,
            Send_Invoices_By__c = 'Mail',
            Apply_Interest__c = FALSE,
            Set_Inactive__c=False,
            Refresher_Amount__c=2345,
            Refresher_Required__c=true,
            Name='Consultations - Non Retainer ',   
            Consultation_Matter__c=True
        );
        matter.add(mattercObj);
        insert(matter);
        
        MonthlyMatterInactive.monthlyMatterInactive();
        test.stopTest();
    }       
    
}
Aneesh AllumallaAneesh Allumalla

Hi Nikitha,

I see that you have not used Assert statement anywher in your test class.

Use System.AssertEquals in your test class after your call to mainclass method, to check if Set_Inactive__c is made to true for inserted record.

Then your coverage will be increased.


Hope this helps.

Please let me know if you have  any questions.

Please mark this as best answer if you find it helpful.

Nikita KapratwarNikita Kapratwar
Hi Aneesh,
I tried using System.AssertEquals but it still showing the same coverage as before.
can u suggest changes in test class to cover the following statement from the apex class :
  list<matter__c> matter= [Select Id, OwnerId, Client__c, Prospect__c, Billing_RateLU__c from Matter__c where Consultation_Matter__c = TRUE and Set_Inactive__c = FALSE Order By CreatedDate DESC Limit 2];
 
Aneesh AllumallaAneesh Allumalla

Hi Nikita,
Can you share a snapshot of code coverage of your main class,

Like what all lines are covered and which are not covered please

Aneesh AllumallaAneesh Allumalla
Also Can you please tell me what is the need for using that Map. Are you using the returned Map anywhere?
Nikita KapratwarNikita Kapratwar
Hi Aneesh,
No Thats only statement for Map, havn't used it anywhere else.
and also I have attached the code coverage below  :

User-added image
mukesh guptamukesh gupta
Hi Nikita,

Please use below code:-
@istest
public class MonthlyMatterInactiveTest
{
    @istest  
    public static void testMethod2() {
        // code_block    
        // 
        test.startTest();
		
		 Monthly_Matter_Execution__c mme = new Monthly_Matter_Execution__c();
		 mme.Date_Executed__c = Date.Today().addMonths(-1);
		 
		 INSERT mme;
 
		
        Account acct = new Account(
            FirstName='test rohini',
            LastName='test twopir'
        ); 
        insert acct;
        
        Propsect__c prop =new Propsect__c(
            Spouses_First_Name__c='test',
            Spouses_Name__c='test',Person__c=acct.id);
        insert prop;
        

        list<matter__c> matter= new list<matter__c>();
        // matter.add(mat);
        
        Matter__c mattercObj = new Matter__c (
            Client__c = acct.id,  
            Prospect__c = prop.id,
            Status__c = 'Active',
            Type__c = 'Consultation',
            Apply_Fixed_Fee__c = FALSE,
            Fixed_fee_Amount__c = NULL,
            Send_Invoices_By__c = 'Mail',
            Apply_Interest__c = FALSE,
            Set_Inactive__c=False,
            Refresher_Amount__c=2345,
            Refresher_Required__c=true,
            Name='Consultations - Non Retainer ',   
            Consultation_Matter__c=True
        );
        matter.add(mattercObj);
        insert(matter);
        
        MonthlyMatterInactive.monthlyMatterInactive();
        test.stopTest();
    }       
    
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Nikita KapratwarNikita Kapratwar
Hi Mukesh,
Thanx for the code , bt Is it throwing this error at insert(matter); line : System.ListException: List index out of bounds: 1 
Aneesh AllumallaAneesh Allumalla

Hi Nikita,

The List you are retreiving from the Main class, contains 2 records, and you are using the second record from List and updating the changes.

But in your test class, you are inserting one record, and your changes wont apply to that record .

try inserting 2 records one after the other(insert first record , then insert second record). Then use System.AsserEquals on first inserted record.

it should pass.
 

@istest
public class MonthlyMatterInactiveTest
{
    @istest  
    public static void testMethod2() {
        // code_block    
        //
        test.startTest();
        Account acct = new Account(
            FirstName='test rohini',
            LastName='test twopir'
        );
        insert acct;
       
        Propsect__c prop =new Propsect__c(
            Spouses_First_Name__c='test',
            Spouses_Name__c='test',Person__c=acct.id);
        insert prop;
       
        /* list<matter__c> matter= new list<matter__c>();
        // matter.add(mat); */
       
        Matter__c mattercObj = new Matter__c (
            Client__c = acct.id,  
            Prospect__c = prop.id,
            Status__c = 'Active',
            Type__c = 'Consultation',
            Apply_Fixed_Fee__c = FALSE,
            Fixed_fee_Amount__c = NULL,
            Send_Invoices_By__c = 'Mail',
            Apply_Interest__c = FALSE,
            Set_Inactive__c=False,
            Refresher_Amount__c=2345,
            Refresher_Required__c=true,
            Name='Consultations - Non Retainer ',  
            Consultation_Matter__c=True
        );
       insert mattercObj;
       Matter__c mattercObj1 = new Matter__c (
            Client__c = acct.id,  
            Prospect__c = prop.id,
            Status__c = 'Active',
            Type__c = 'Consultation',
            Apply_Fixed_Fee__c = FALSE,
            Fixed_fee_Amount__c = NULL,
            Send_Invoices_By__c = 'Mail',
            Apply_Interest__c = FALSE,
            Set_Inactive__c=False,
            Refresher_Amount__c=24355,
            Refresher_Required__c=true,
            Name='Consultations - Non Retainer ',  
            Consultation_Matter__c=True
        );
        insert mattercObj1;
        MonthlyMatterInactive.monthlyMatterInactive();
        System.assertEquals(True,mattercObj.Set_Inactive__c);
        test.stopTest();
    }      
   
}

Hope this works.

Please mark as best answer if it helped

Nikita KapratwarNikita Kapratwar
hi Aneesh,
it is not covering the red part still the coverage is 36%, and the code is throwing this error :
System.AssertException: Assertion Failed: Expected: true, Actual: false
Aneesh AllumallaAneesh Allumalla

Hi Nikita,

Make the below change in your main class.

replace if(mmeMap.size() != 0) with if(mmeMap.size() > 0).

 

And use this below test class :
@istest
public class MonthlyMatterInactiveTest
{
    @istest  
    public static void testMethod2() {
        // code_block    
        //
        test.startTest();
        Monthly_Matter_Execution__c mme = new Monthly_Matter_Execution__c();
        mme.Date_Executed__c = Date.Today().addMonths(-1);
       
        INSERT mme;
        Account acct = new Account(
            FirstName='test rohini',
            LastName='test twopir'
        );
        insert acct;
       
        Propsect__c prop =new Propsect__c(
            Spouses_First_Name__c='test',
            Spouses_Name__c='test',Person__c=acct.id);
        insert prop;
       
        /* list<matter__c> matter= new list<matter__c>();
        // matter.add(mat); */
       
        Matter__c mattercObj = new Matter__c (
            Client__c = acct.id,  
            Prospect__c = prop.id,
            Status__c = 'Active',
            Type__c = 'Consultation',
            Apply_Fixed_Fee__c = FALSE,
            Fixed_fee_Amount__c = NULL,
            Send_Invoices_By__c = 'Mail',
            Apply_Interest__c = FALSE,
            Set_Inactive__c=False,
            Refresher_Amount__c=2345,
            Refresher_Required__c=true,
            Name='Consultations - Non Retainer ',  
            Consultation_Matter__c=True
        );
       insert mattercObj;
       Matter__c mattercObj1 = new Matter__c (
            Client__c = acct.id,  
            Prospect__c = prop.id,
            Status__c = 'Active',
            Type__c = 'Consultation',
            Apply_Fixed_Fee__c = FALSE,
            Fixed_fee_Amount__c = NULL,
            Send_Invoices_By__c = 'Mail',
            Apply_Interest__c = FALSE,
            Set_Inactive__c=False,
            Refresher_Amount__c=24355,
            Refresher_Required__c=true,
            Name='Consultations - Non Retainer ',  
            Consultation_Matter__c=True
        );
        insert mattercObj1;
        MonthlyMatterInactive.monthlyMatterInactive();
        System.assertEquals(True,mattercObj.Set_Inactive__c);
        test.stopTest();
    }      
   
}