You need to sign in to do that
Don't have an account?
Test class for Batch Apex with coverage
Hi Guys, Need help in coverage.I have written test class for below batch apex biut was able to achieve only 50% coverage.Pls help me to get 100% coverage. global class BatchClassexample implements Database.Batchable<sObject> { global String query = 'SELECT Id,Name FROM Account where name=:filter '; global Database.QueryLocator start(Database.BatchableContext BC) { system.debug('inside methiod----'); String filter='bBVINES'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<Account> scope) { //User u=[SELECT username from user where profileid='00e28000000sxOR']; system.debug('inside method 1---'); for(Account a : scope) { a.Name = a.Name + 'By bhuvanBam'; } system.debug(scope); update scope; //system.runAs(u){} } global void finish(Database.BatchableContext BC) { } } //Test class @isTest public class BatchClassexampleTestClass { public static testMethod void testRunAs(){ String filter='bBVINES'; Test.startTest(); BatchClassexample BC=new BatchClassexample(); Account a=new Account(name='bBVINES'); insert a; List<Account> alist=[Select name from Account where name like '%bBVINES%']; for(Account afor:alist){ afor.name=afor.name+'Bhuvanbam'; } update alist; User u=[SELECT username from user where profileid='00e28000000sxOR']; system.runAs(u){} DataBase.executeBatch(BC,200); Test.stopTest(); } }
Remove update statement in your test class and try
//Test class
@isTest
public class BatchClassexampleTestClass {
public static testMethod void testRunAs(){
String filter='bBVINES';
Test.startTest();
BatchClassexample BC=new BatchClassexample();
Account a=new Account(name='bBVINES');
insert a;
User u=[SELECT username from user where profileid='00e28000000sxOR'];
system.runAs(u){}
DataBase.executeBatch(BC,200);
Test.stopTest();
}
}