You need to sign in to do that
Don't have an account?

Test Coverage If/Else Trigger 28%
I'm trying to write a test class for the following trigger, but I'm only covering 28% of the lines. How can I edit it to cover the 4 if/else conditions?
My Trigger:
And the test class so far...(The trigger is designed to assign an AccountNumber stored in a custom Counter setting)
Thanks!
My Trigger:
trigger AccountNumberIndex on Opportunity(before insert,before update){ List<Id> accIds = new List<Id>(); for(Opportunity opp:trigger.new){ if(opp.AccountId!=null){ accIds.add(opp.AccountId); } } Map<Id,Account> accMap = new Map<Id,Account>([select id,LC_Region__c,AccountNumber,Type from Account where id in:accIds]); Counterr__c value = new Counterr__c(); for(Counterr__c record:[SELECT Id,Ones__c,Threes__c,Eights__c,Nines__c FROM Counterr__c FOR UPDATE]) { value = record; for(Opportunity opp :trigger.new){ if(!accMap.IsEmpty()){ if((opp.Probability == 90) && (accMap.get(opp.AccountId).AccountNumber == null)){ if((accMap.get(opp.AccountId).LC_Region__c == 'North America')&&(accMap.get(opp.AccountId).Type != 'Prospect')){ accMap.get(opp.AccountId).AccountNumber = String.valueOf(value.Ones__c); value.Ones__c+= 1; } else if ((accMap.get(opp.AccountId).LC_Region__c == 'North America')&&(accMap.get(opp.AccountId).Type == 'Prospect')){ accMap.get(opp.AccountId).AccountNumber = String.valueOf(value.Threes__c); value.Threes__c+= 1; } else if ((accMap.get(opp.AccountId).LC_Region__c == 'International')&&(accMap.get(opp.AccountId).Type != 'Prospect')){ accMap.get(opp.AccountId).AccountNumber = String.valueOf(value.Eights__c); value.Eights__c+= 1; } else if ((accMap.get(opp.AccountId).LC_Region__c == 'International')&&(accMap.get(opp.AccountId).Type == 'Prospect')){ accMap.get(opp.AccountId).AccountNumber = String.valueOf(value.Nines__c); value.Nines__c+= 1; } update value; } update accMap.values(); } } } }
And the test class so far...(The trigger is designed to assign an AccountNumber stored in a custom Counter setting)
@isTest Private Class UnitTest_AccountNumberIndex{ static testMethod void LCUnitTest(){ /*Create Counter*/ Counter__c Count= new Counter__c(); Count.Name='Counter'; Count.Ones__c=101000; /*Create User/America/Account*/ Account a1 = new Account(); a1.name='Test Account1'; a1.Type= 'Customer-Direct'; a1.LC_Region__c='North America'; a1.AccountNumber=null; insert a1; /*Create Opp1*/ Opportunity o1=new Opportunity(); o1.Name='Test Opp1'; o1.AccountId=a1.Id; o1.closeDate=date.today(); o1.StageName='Perception Analysis'; o1.Probability=70; insert o1; o1.StageName='Negotiation/Review'; o1.Probability=90; update o1; a1.AccountNumber=String.valueOf(Count.Ones__c); update a1; System.assertEquals(String.valueOf(Count.Ones__c),a1.AccountNumber); } }
Thanks!
You have covered one of the if condition with satisifed values. Create separate methods for each of conditions and insert data like what you have inserted but change the values in the data according your condition match.
So insert all of the related records in different methods you will get code coverage... The above code is sample one.
All Answers
You have covered one of the if condition with satisifed values. Create separate methods for each of conditions and insert data like what you have inserted but change the values in the data according your condition match.
So insert all of the related records in different methods you will get code coverage... The above code is sample one.