This is my batch class:
global class changeMultipleContactName implements Database.Batchable< sObject >
{
global String name;
global String n = 'Helly';
global Database.QueryLocator start( Database.BatchableContext BC )
{
name = ' SELECT LastName FROM Contact WHERE LastName =:n ';
return Database.getQueryLocator( name );
}
global void execute( Database.BatchableContext BC, List< Contact > scope )
{
List<Contact> con = NEW List<Contact>();
for(Contact c:scope)
{
c.LastName = 'roz';
con.add( c );
}
update con;
}
global void finish( Database.BatchableContext BC )
{
}
}
This is my Test class:
@isTest
public class changeMultipleContactName_Test
{
public static testMethod void test()
{
Contact con= NEW Contact();
con.LastName = 'Mahindra';
insert con;
test.startTest();
changeMultipleContactName job = NEW changeMultipleContactName ( );
job.name= 'SELECT LastName FROM Contact WHERE LastName WHERE LastName =:n ';
Database.executeBatch( job );
test.stopTest();
}
}
Here code coverage for the batch class is 41% only and the batch class is not executing.please give me solution for this.
Best Answer
Setting
in your test will result in a record being returned and so more coverage.
However, the batchable would be better written like this:
so that it could be called like this: