0

I have a batch class which updates lead source and a test class which executes this batch class.But in my case the batch class is not getting executed from the test class.The batch is not even getting executed even once.

LeadProcessor class

global class LeadProcessor implements Database.Batchable<Sobject>,Database.Stateful {
    global DataBase.QueryLocator start(DataBase.BatchableContext BC){
        return DataBase.getQueryLocator(
            'Select LeadSource from lead limit 10'
        );
    }
    global void execute(Database.BatchableContext BC,list<lead> leadlist){ 
        List<lead> newleadlist=new list<lead>();
        for(lead l :leadlist){
            l.LeadSource='DreamForce';
            newleadlist.add(l);
        }
        update newleadlist;
    }
    global void finish(DataBase.BatchableContext BC){

    }
}

LeadProcessor Test class

@isTest global class LeadProcessorTest {

    @isTest global static void LeadProcessorTestMethod(){
       list<lead> leadlist=new list<lead>();
        leadlist.add(new lead(lastname='Jon',company='mojito sns',status='Open'));
        insert leadlist;   
    }
    @isTest global static void TestRun(){
        Test.StartTest();
        LeadProcessor LP=new LeadProcessor();
        id jobid=DataBase.executeBatch(LP);
        Test.StopTest();
        system.debug(jobid);
    }

}
2

2 Answers 2

2

The reason your batch is not being executed is because you do not create your test data.

@IsTest
static void testMyBatch()
{
    insert new Lead(LastName='...', Company='...', Status='...');
    // now your batch will run
}

Alternatively, use the @TestSetup annotation. Annotating your setup as @IsTest just runs it as another test method.

@TestSetup
static void setup()
{
    insert new Lead(LastName='...', Company='...', Status='...');
}
@IsTest
static void testMyBatch()
{
    // now your batch will run
}

Please also note from How to Write Good Unit Tests (emphasis mine):

Verify the results are correct

Verifying that your code works as you expect it to work is the most important part of unit testing. It’s also one of the things that Force.com developers commonly neglect. Unit tests that do not verify the results of the code aren’t true unit tests. They are commonly referred to as smoke tests, which aren’t nearly as effective or informative as true unit tests.

A good way to tell if unit tests are properly verifying results is to look for liberal use of the System.assert() methods. If there aren’t any System.assert() method calls, then the tests aren’t verifying results properly. And, no, System.assert(true); doesn’t count.

7
  • Yes i added @testSetup..but still the records are not getting created.And even batch is not running Commented Jul 5, 2017 at 13:29
  • How do you know the records aren't getting created? You don't have a single assertion. And you said in other comments you get 100% coverage, so what do you mean the batch doesn't run?
    – Adrian Larson
    Commented Jul 5, 2017 at 13:30
  • In my batch class i have changed the status of leadsource to 'DreamForce' but when i am checking through query or org the record is not modified with that leadsource. Commented Jul 5, 2017 at 13:34
  • Unit tests do not modify your org data...
    – Adrian Larson
    Commented Jul 5, 2017 at 13:35
  • 1
    @DeepakAgarwal "is it sufficient that my test class showed 100% coverage" NO it is not sufficient. Without asserts all you know is that your test method did not cause an unhandled exception.
    – Eric
    Commented Jul 5, 2017 at 13:41
0

You have two methods until you use @testSetup as commented you don't see the data created in other class.

// Option 1
@isTest global class LeadProcessorTest {

    @isTest global static void LeadProcessorTestMethod(){
       list<lead> leadlist=new list<lead>();
        leadlist.add(new lead(lastname='Jon',company='mojito sns',status='Open'));
        insert leadlist;   
    }
    @isTest global static void TestRun(){
     LeadProcessorTestMethod();
     Test.StartTest();
     LeadProcessor LP=new LeadProcessor();
        id jobid=DataBase.executeBatch(LP);
        Test.StopTest();
        system.debug(jobid);
    }

}

// Option 2

@isTest global class LeadProcessorTest {
    @testSetup static void setup() {
        // Create common test records
        list<lead> leadlist=new list<lead>();
        leadlist.add(new lead(lastname='Jon',company='mojito sns',status='Open'));
        insert leadlist;        
    }
    @isTest global static void TestRun(){
     LeadProcessor LP=new LeadProcessor();
        id jobid=DataBase.executeBatch(LP);
        Test.StopTest();
        system.debug(jobid);
    }

}

You must log in to answer this question.

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