You need to sign in to do that
Don't have an account?
vinod kumar 364
Not getting test class code coverage for If() condition
I tried several times to get the code coverage for the below test class now I need someone's help to get code coverage for red color coding in the Image How can I ???
Here the below image shows the code coverage.
My trigger
Test class
Here the below image shows the code coverage.
My trigger
trigger customsolinsert on Price_Study__c (after update, after insert) { // Find the existing (0 or 1) Custom_solution__c that reference Price_Study__c Map<Id, Custom_solution__c> m = new Map<Id, Custom_solution__c>(); list<Custom_solution__c> oblist=[ select id, Price_Study__c from Custom_solution__c where Price_Study__c in :Trigger.newMap.keySet() ]; for (Custom_solution__c ob : oblist) { m.put(ob.Price_Study__c, ob); } // Insert or update the Custom_solution__c List<Custom_solution__c> csol= new List<Custom_solution__c>(); for (Price_Study__c b : trigger.new) { // Get record to update Custom_solution__c ob = m.get(b.id); if(b.Analysis_Done_By__c<>Null){ if (ob == null) { // If no record to update, add a record to be inserted ob = new Custom_solution__c(Price_Study__c = b.id); } ob.Price_Study__c=b.Id; ob.Country__c=b.Country_of_Treatment__c; ob.Who_Deals__c=b.Analysis_Done_By__c; ob.Customer__c=b.Account_Name__c; ob.Product_line__c=b.Product_line__c; ob.Function_Focus__c=b.Function_Focus__c; ob.Comments__c=b.Generic_Comments__c; ob.Nbrs_Post__c=b.Nbrs_Post__c; ob.Series_Row_nbr__c=b.Series_Row_nbr__c; csol.add(ob); } } upsert csol;
Test class
@istest(isParallel=true) public class Testcustomsolinsert{ @istest Static void Testcustomsol1(){ Profile prof = [select id from profile where name='system Administrator']; List<User> lstUser = [Select u.Profile.Name, u.ProfileId, u.IsActive, u.Id From User u Where IsActive = true AND Profile.Name = 'System Administrator']; system.runAs(lstUser[0]){ Account acc=new account(Name='NicoTestacc',BillingCountry='India'); insert acc; Opportunity op1= new opportunity(Name='NicoTestOpp',CloseDate=date.today(),StageName='Qualification',Product_Type__c='DPI',Accountid=acc.Id); insert op1; System.assertEquals(op1.name,'NicoTestOpp'); Price_Study__c ps=new Price_Study__c(Country_of_Treatment__c='India',Series_Row_nbr__c=3, Nbrs_Post__c=2,Analysis_Done_By__c=op1.OwnerId, Generic_Comments__c='asdfds',Function_Focus__c='sdad', Product_line__c='CMM'); Insert Ps; Map<Id, Custom_solution__c> m = new Map<Id, Custom_solution__c>(); Custom_solution__c cs1=new Custom_solution__c(Price_Study__c=PS.Id,Country__c=Ps.Country_of_Treatment__c); Insert cs1; delete cs1; Custom_solution__c cs=new Custom_solution__c(); cs.Price_Study__c=ps.Id; cs.Country__c=Ps.Country_of_Treatment__c; cs.Who_Deals__c=ps.Analysis_Done_By__c; cs.Customer__c=ps.Account_Name__c; cs.Product_line__c='Cmm'; cs.Function_Focus__c=ps.Function_Focus__c; cs.Comments__c=ps.Generic_Comments__c; cs.Nbrs_Post__c=ps.Nbrs_Post__c; cs.Series_Row_nbr__c=ps.Series_Row_nbr__c; Insert cs; cs.Function_Focus__c=ps.Function_Focus__c; Update cs; } } }
Let us know if this will help you
All Answers
To cover any "if" condition you need to do two type of testing
1. Negative
2. Positive
Here I can see you have writted a test method "Testcustomsol1" which is covered negative scenario of below "if" condition
if (ob == null)
To make sure it will cover completely you need to create another test method in which
if (ob == null) satisfied.
i.e. Custom_solution__c should be null.
Hope it'll help.
Mhalo,
Aamir AH Khan
Lead Salesforce Lightning Developer
I need your help some example code
Problem with the opportunity ownerId you are trying to assign it to Price Study object. It assigned as null value to the field "Analysis_Done_By__c". Please refer below code:
Thanks & Regards
Gulshan Raj
Sr. Developer
Let us know if this will help you
I don't understand what happened can you please explain me in a clear way? anyway, it's working thank you so much.
When you trying to insert opportunity record the value of OwnerID didn't populate by default. When you do SOQL by mentioning OwnerID explicitly it will bring the id opportunity owner.