You need to sign in to do that
Don't have an account?
Amit 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*/
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?
- 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'); } }
Can you try something like below: If this information helps, please mark the answer as best. Thank you
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.
Below Test Class have 100% code coverage :
Apex Class :
Test Class : Hope this will be helpful.
Thanks!