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
RajiiiRajiii 

Test method code coverage Problem for batch apex

I have written test method for Batch apex. Like the code below

 

    Test.startTest();

    TestBatchclass job = new TestBatchclass();

    ID batchprocessid = Database.executeBatch(job);

    Test.stopTest();

 

It covered only 27% to my respective batch. So I have created sample records also. But code covered only 27%.

Those four lines alone also covered 27%. How to proceed this.

Best Answer chosen by Admin (Salesforce Developers) 
Jeff MayJeff May

If you look at the coverage results for the class, you'll be able to see which lines were covered and which were not.  You can fine-tune your test class by adding a public String variable to the apex class and put your queryString there.  This let's you set a different queryString in your test.

 

class myClass {

 

  public String myQuery = 'SELECT Id, Name from Lead where IsActive=true';

 

  // batch methods

 

 

  testmethod myTest(){

 

        myClass mc = new MyClass();

        mc.myQuery = 'SELECT Id, Name from Lead where IsActive=true and Email = \'hello@me.com\'';

        Database.executeBatch(mc);

 

 

  }

 

}

All Answers

Jeff MayJeff May

If you look at the coverage results for the class, you'll be able to see which lines were covered and which were not.  You can fine-tune your test class by adding a public String variable to the apex class and put your queryString there.  This let's you set a different queryString in your test.

 

class myClass {

 

  public String myQuery = 'SELECT Id, Name from Lead where IsActive=true';

 

  // batch methods

 

 

  testmethod myTest(){

 

        myClass mc = new MyClass();

        mc.myQuery = 'SELECT Id, Name from Lead where IsActive=true and Email = \'hello@me.com\'';

        Database.executeBatch(mc);

 

 

  }

 

}

This was selected as the best answer
RajiiiRajiii

Thanks for your reply JeffM,

Here querylocator contains query like  [Select id, Name from User where Profile.Name = 'Test Customer'].

When i am trying to format the query string there is a problem on single quotes. So how to form the above query as a string

 

String query = 'Select id, Name from User where Profile.Name = 'Test Customer' ';

Jeff MayJeff May
you can use backslash: Profile.Name \'Test Customer\' '
RajiiiRajiii

Thanks for ur help, it almost covered. but it is now 52%

I have two conditions in my batch tht mean one for 'Test customer' another 'Test customer2'. Where i given a query for Test customer2, the Test customer condition also covered. how it is possible? What is the cycle for this execution.?

 

Like the query is 'Select id, name from User where profile.name = 'Test Customer2';

Jeff MayJeff May

you can add an AND to your WHERE clause so you get both customers in the batch submitted to the test.