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
Amit Karlekar 4Amit Karlekar 4 

How to improve the code coverage of the test class?

/*Future Method 3: Upon Account record creation if the industry has the value as 'Media' or 'Enery' then populate the Rating as 'Hot'.

- Create Class With Fututre Method
- Call it from an anonymous window
- Create a Test Class*/

 
public class FutureMethodPopulateRatingAsHot {

    @future
    public static void populateRaingAsHot(List<Id> accIds) {
        
        List<Account> accRecords = [SELECT Id, Industry, Rating
                                   FROM Account WHERE (Industry = 'Media'
                                   OR Industry = 'Energy') AND Rating != 'Hot'];
        
        if(!accRecords.isEmpty()) {
            for(Account acc : accRecords) {
                acc.Rating = 'Hot';
            }
        }
        update accRecords;
    }
}
---------------------------------------------------------------------------------------------

For the above future method, the test class I wrote is giving only 66% code coverage.

I tried a lot, but not able to get maximum code coverage. Can somebody please help?

 
@isTest
private class FutureMethodPopulateRatingAsHotTest {
    
    @isTest
    static void populateRaingAsHotTest() {
        
        List<Account> accList = new List<Account>();
        
        for(Integer i=1; i<=100; i++) {
            Account acc = new Account();
            acc.Name= 'Test Name' + i;
            acc.Industry = 'Energy';
            acc.Rating = 'Warm';
            
            accList.add(acc);
            
            Account acc2 = new Account();
            acc2.Name = 'Test Name2 ' + i;
            acc2.Industry = 'Media';
            acc.Rating = 'Warm';
            
            accList.add(acc2);
        }
        Test.startTest();
        insert accList;
        
        List<Id> accIds = new List<Id>();
        
        for(Account acc : accList) {
            if(acc.Industry == 'Energy' || acc.Industry == 'Media') {
                acc.Rating = 'Hot';
                accIds.add(acc.Id);
            }
        }
        update accList;
        
        FutureMethodPopulateRatingAsHot.populateRaingAsHot(accIds);
        Test.stopTest();
        
        List<Account> accRecords = [SELECT Id, Industry, Rating
                                    FROM Account WHERE Id =: accList[0].Id];
        
        System.assertEquals('Hot', accRecords[0].Rating, 'Rating Not Updated');
    }
}

 
AshwiniAshwini (Salesforce Developers) 
Hi Amit,
Can you try something like below:
@isTest
private class FutureMethodPopulateRatingAsHotTest {
    
    @isTest
    static void populateRaingAsHotTest() {
        
        List<Account> accList = new List<Account>();
        
        for(Integer i=1; i<=100; i++) {
            Account acc = new Account();
            acc.Name= 'Test Name' + i;
            acc.Industry = 'Energy';
            acc.Rating = 'Warm';
            
            accList.add(acc);
            
            Account acc2 = new Account();
            acc2.Name = 'Test Name2 ' + i;
            acc2.Industry = 'Media';
            acc2.Rating = 'Warm';
            
            accList.add(acc2);
        }
        
        // Insert accounts
        Test.startTest();
        insert accList;
        Test.stopTest();
        
        // Ensure some accounts have 'Hot' rating
        List<Account> accountsWithHotRating = [SELECT Id, Industry, Rating FROM Account WHERE Rating = 'Hot'];
        System.assert(!accountsWithHotRating.isEmpty(), 'No accounts with Rating = Hot');
        
        // Calling  future method
        List<Id> accIds = new List<Id>();
        for(Account acc : accList) {
            if(acc.Industry == 'Energy' || acc.Industry == 'Media') {
                accIds.add(acc.Id);
            }
        }
        FutureMethodPopulateRatingAsHot.populateRaingAsHot(accIds);
        List<Account> updatedAccounts = [SELECT Id, Industry, Rating FROM Account WHERE Id IN :accIds];
        for(Account acc : updatedAccounts) {
            System.assertEquals('Hot', acc.Rating, 'Rating Not Updated');
        }
        
        List<Account> accountsWithOtherIndustry = [SELECT Id, Industry, Rating FROM Account WHERE Industry NOT IN ('Energy', 'Media')];
        for(Account acc : accountsWithOtherIndustry) {
            System.assertEquals('Warm', acc.Rating, 'Rating Updated for Unexpected Industry');
        }
    }
}
If this information helps, please mark the answer as best. Thank you
 
Amit Karlekar 4Amit Karlekar 4

Hi Ashwini,

Thank you so much for taking some time off from your scedule and taking efforts to help me solve the problem.

Unfortunitely, the problem remains as it is. Tried your provided code, it is passing successfully but, still covering only 66% of the code. Believe me when I say I tried a lot in different ways but couldn't get it done perfectly. See if you can get it done with at least 75% as it is required, that will be a big help.

User-added image

Arun Kumar 1141Arun Kumar 1141
Hi Amit,

Below Test Class have 100% code coverage :

Apex Class :
User-added image

Test Class :
@isTest
public class FutureMethodPopulateRatingAsHotTest {
    @TestSetup static void setup() {
        List<Account> accList = new List<Account>();
        for(Integer i = 0; i < 250; i++) {
            Account acc = new Account(Name='Test ' + i);
            if(i < 50) {
                acc.Industry = 'Media';
                acc.Rating = 'Hot';
            } else if ( i > 50 && i < 100 ) {
                acc.Industry = 'Energy';
                acc.Rating = 'Hot';
            } else if ( i > 100 && i < 150 ) {
                acc.Industry = 'Media';
            } else if ( i > 150 && i < 200 ) {
                acc.Industry = 'Energy';
            }
            accList.add(acc);
        }
        insert accList;
    }
    @isTest static void populateRaingAsHotTest() {
        List<Account> accList = [Select Id, Industry, Rating From Account limit 250];
        List<Id> accIds = new List<Id>();
        for(Account acc : accList) { 
            accIds.add(acc.Id);
        } 
        Test.startTest();
        FutureMethodPopulateRatingAsHot.populateRaingAsHot(accIds);
        System.assertEquals(98, [SELECT Count()
                                 FROM Account WHERE (Industry = 'Media'
                                                     OR Industry = 'Energy') AND Rating != 'Hot']);
        Test.stopTest();
    }
}
Hope this will be helpful.
Thanks!