+ Start a Discussion
R@v!R@v! 

how to write test class for batch class.

global class UserLogJob implements Schedulable {

       global void execute(SchedulableContext SC) {

          EmailUserLogController M = new EmailUserLogController();
          m.UserLog();

       }

    }
Best Answer chosen by R@v!
Amit Chaudhary 8Amit Chaudhary 8
Hi Ravi,

Please try below Test class i hope that will help you
@isTest
private class UserLogJobTest
{
	public static String CRON_EXP = '0 0 0 15 3 ? 2022';

    static testmethod void test() 
    {
		Test.startTest();

			String jobId = System.schedule( 'UserLogJobTest', CRON_EXP, new UserLogJob() );
			CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
			System.assertEquals(CRON_EXP, ct.CronExpression);
			System.assertEquals(0, ct.TimesTriggered);
			
		Test.stopTest();
	}
}
Let us know if this will help you

 

All Answers

sandeep@Salesforcesandeep@Salesforce
@isTest
private class UserLogJobTest  {
    static testMethod void userTest() {
        UserLogJob M = new UserLogJob();
         Test.startTest();
        Database.executeBatch(M,1);
        Test.stopTest();
    }
}

Thanks 
Sandeep Singhal
R@v!R@v!
but it throw an error at line 6 column 9


Error: Argument must be an object that implements Database.Batchable
Amit Chaudhary 8Amit Chaudhary 8
Hi Ravi,

Please try below Test class i hope that will help you
@isTest
private class UserLogJobTest
{
	public static String CRON_EXP = '0 0 0 15 3 ? 2022';

    static testmethod void test() 
    {
		Test.startTest();

			String jobId = System.schedule( 'UserLogJobTest', CRON_EXP, new UserLogJob() );
			CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
			System.assertEquals(CRON_EXP, ct.CronExpression);
			System.assertEquals(0, ct.TimesTriggered);
			
		Test.stopTest();
	}
}
Let us know if this will help you

 
This was selected as the best answer
R@v!R@v!
Thank u amit
Krystal D.  CarterKrystal D. Carter
Hi I'm getting the same error.  Any help here woul be greatly appreciated!  I've bolded the line where I'm getting the "Error: Argument must be an object that implements Database.Batchable"

@istest
public class  testAgreementsharecalc
{
     Public static testMethod void testingagreementsharecalc()
     {
        Test.startTest();
        agreementsharecalc sb = new agreementsharecalc();
        Apttus__APTS_Agreement__c objAgreement = new Apttus__APTS_Agreement__c();
        objAgreement.Apttus__Requestor__c = UserInfo.getUserId();
        insert objAgreement;
        
        ID batchprocessid = Database.executeBatch(sb);          
        Test.stopTest();
     }
}