You need to sign in to do that
Don't have an account?
Mayank Srivastava (Salesforce fan)
Need to increase code coverage on the test class. Inputs?
I wrote an Apex trigger and a test class for updating two fields on Accounts depending on some actions on the custom QA Release object. I am unable to get the code coverage reach 75% for some reason. Any inputs on what I might be doing wrong?
Apex Trigger
Test Class
Result when I test coverage:
Apex Trigger
trigger UpdateVersionsOnAccounts on QA_Release__c (after update) { set <id> qaRelIds = new set<id>(); set<id> accIds=new set<id>(); for(QA_Release__c qa:trigger.new) { qaRelIds.add(qa.Client__c); accIds.add(qa.Client__c); } List<QA_Release__c> qaList=[SELECT Client__c,Objective__c,Client_Site_Type__c,Delivered_Date__c,Release_Version__c FROM QA_Release__c WHERE Client__c=:qaRelIds]; List<account> accList=[SELECT Id,Production_Version__c,UAT_Version__c FROM Account where Id in:accIds]; map <id,QA_Release__c> qaMap =new map<id,QA_Release__c>(); for(QA_Release__c qa:qaList) { qaMap.put(qa.Client__c,qa); } for( Account acc:accList){ system.debug('Entered Account list with new trigger collection'); if(qaMap.containskey(acc.Id)) { date delDate=qaMap.get(acc.Id).Delivered_Date__c; string relVer=qaMap.get(acc.Id).Release_Version__c; string clInst=qaMap.get(acc.Id).Client_Site_Type__c; string objective=qaMap.get(acc.Id).Objective__c; if(delDate!=NULL) { if(objective=='New Build' && clInst=='Production') acc.Production_Version__c=relVer; else if(objective=='New Build' && clInst=='QA') acc.UAT_Version__c=relVer; } } update accList; } }
Test Class
@isTest public class TestUpdateVersionsOnAccounts { static testMethod void insertnewQARelease() { QA_Release__c qa= new QA_Release__c(); qa.Client__c='001M000000ce9DkIAI'; qa.Objective__c='New Build'; qa.Due_Date__c=datetime.NOW(); qa.Client_Site_Type__c='Production'; insert qa; system.debug('QA Release Id is:'+qa.Name); //Updating the QA task qa.Status__c='Completed'; qa.Assignee__c='005C0000003SiStIAK'; qa.Release_Version__c='6.5.0.0'; update qa; qa.Delivered_Date__c=date.TODAY(); update qa; } }
Result when I test coverage:
You need to create an Account as part of your test class, and then call the Id from that account as your qa.Client__c when you create the QA_Release__c record. All your class is doing right now is calling a hardcoded Account Id.
All Answers
You need to create an Account as part of your test class, and then call the Id from that account as your qa.Client__c when you create the QA_Release__c record. All your class is doing right now is calling a hardcoded Account Id.