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
suji srinivasansuji srinivasan 

how to achieve100%test coverage for this batch class.

global class UpdateRating implements Database.Batchable<sObject>, Database.Stateful {
    global Integer recordsProcessed = 0;
 global  Database.QueryLocator  start(Database.BatchableContext bc) {
    string query='select Rating,(select StageName from Opportunities where StageName=\''+String.escapeSingleQuotes('Inprogress,Delivered,closedwon')+ '\') From account where Rating <> \''+String.escapeSingleQuotes('client') + '\'';
     return Database.getQueryLocator(query); 
    }
     global Void execute(Database.BatchableContext bc, List<Account> Scope){
      
        for(Account a :Scope){
            a.Rating='client';        
            recordsProcessed = recordsProcessed + 1;
        }
         update Scope; }
         global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + 'records processed. Shazam!');
            
    }   
     }

test class

@isTest public class UpdateAccountRatingTest {
@testSetup static void setup() {

Account ac =new Account();
ac.Name = 'test';
ac.rating='prospect';
insert ac;
ac.Name = 'test';
ac.rating='client';
update ac; }
@isTest static void test() {
Test.startTest();
UpdateAccountRating uar = new UpdateAccountRating();
Id batchId = Database.executeBatch(uar);
Test.stopTest(); } }
Thanks in advance
Best Answer chosen by suji srinivasan
ravi soniravi soni
Hi Suji,
Find your test class with 100% Coverage and with best practice.
@isTest
public class UpdateRatingTest {
    @isTest
    public static void unit_test(){
        //insert All mandatory fields
        Account acc =new Account();
        acc.Name = 'test';
        acc.rating='Hot';
        insert acc;
        
        //insert All mandatory fields
        Opportunity opp = new opportunity();
        opp.Name = 'test opp';
        opp.Accountid = acc.Id;
        opp.stageName = 'Inprogress';
        opp.CloseDate = system.today()+5;
        insert opp;
        
        Test.startTest();
        UpdateRating oUpdateRating  = new UpdateRating();
        Database.executeBatch(oUpdateRating);
        Test.stopTest(); 
        list<Account> lstAcc = [SELECT Id,Name,Rating FROM Account Where Id =:acc.Id];
        system.assertEquals('client', lstAcc[0].Rating);
        
    }
}

don't forget to mark it as the best answer.
Thank you

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Suji,

try with below code.
@isTest public class UpdateAccountRatingTest {
 
 static testmethod void AccountRatingmethod() {

Account ac =new Account();
ac.Name = 'test';
ac.rating='prospect';
insert ac;

//insert opp mandatory fields
Opportunity opp1 = new opportunity();
opp1.Name = 'test opp';
opp1.Accountid = ac.id;
opp1.stageName = 'Inprogress';
insert opp1;

Opportunity opp1 = new opportunity();
opp1.Name = 'test opp';
opp1.Accountid = ac.id;
opp1.stageName = 'Delivered';
insert opp1;

Opportunity opp2 = new opportunity();
opp2.Name = 'test opp';
opp2.Accountid = ac.id;
opp2.stageName = 'Delivered';
insert opp2;

Opportunity opp3 = new opportunity();
opp3.Name = 'test opp';
opp3.Accountid = ac.id;
opp3.stageName = 'closed won';
insert opp3;

acc.Rating = 'client';

update acc;
Test.startTest();
UpdateAccountRating uar = new UpdateAccountRating();
Database.executeBatch(uar);
Test.stopTest(); 
} 
}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
suji srinivasansuji srinivasan
Hi Ankaiah, still the code covered 50% only . I need to achieve 100% coverage.
User-added image

start method ,soql and finish methods are not covered. thanks in advance 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Suji,

try with below code getting 100%
 
@isTest public class UpdateAccountRatingTest {
 
 static testmethod void AccountRatingmethod() {

Account ac =new Account();
ac.Name = 'test';
ac.rating='prospect';
insert ac;

//insert opp mandatory fields
Opportunity opp1 = new opportunity();
opp1.Name = 'test opp';
opp1.Accountid = ac.id;
opp1.stageName = 'Inprogress';
opp1.CloseDate = system.today()+5;
insert opp1;

Opportunity opp2 = new opportunity();
opp2.Name = 'test opp';
opp2.Accountid = ac.id;
opp2.stageName = 'Delivered';
opp2.CloseDate = system.today()+5;
insert opp2;

Opportunity opp3 = new opportunity();
opp3.Name = 'test opp';
opp3.Accountid = ac.id;
opp3.stageName = 'closed won';
opp3.CloseDate = system.today();
insert opp3;

ac.Rating = 'client';

update ac;
Test.startTest();
UpdateRating uar = new UpdateRating();
Database.executeBatch(uar);
Test.stopTest(); 
} 
}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
ravi soniravi soni
Hi Suji,
Find your test class with 100% Coverage and with best practice.
@isTest
public class UpdateRatingTest {
    @isTest
    public static void unit_test(){
        //insert All mandatory fields
        Account acc =new Account();
        acc.Name = 'test';
        acc.rating='Hot';
        insert acc;
        
        //insert All mandatory fields
        Opportunity opp = new opportunity();
        opp.Name = 'test opp';
        opp.Accountid = acc.Id;
        opp.stageName = 'Inprogress';
        opp.CloseDate = system.today()+5;
        insert opp;
        
        Test.startTest();
        UpdateRating oUpdateRating  = new UpdateRating();
        Database.executeBatch(oUpdateRating);
        Test.stopTest(); 
        list<Account> lstAcc = [SELECT Id,Name,Rating FROM Account Where Id =:acc.Id];
        system.assertEquals('client', lstAcc[0].Rating);
        
    }
}

don't forget to mark it as the best answer.
Thank you
This was selected as the best answer
suji srinivasansuji srinivasan
Thank you ravi soni.