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
Kon DeleKon Dele 

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:
 
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!
Best Answer chosen by Kon Dele
Arunkumar RArunkumar R
Hi kon,

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.


@isTest
  Private Class UnitTest_AccountNumberIndex{

static testMethod void LCUnitTest(){

      o1.StageName='Negotiation/Review';
      o1.Probability=90; // This will for your first if statement will pass.
      update o1;
  
    }

static testMethod void LCUnitTest1(){

      o1.StageName='Negotiation/Review';
      o1.Probability=20; // This will goes to second statement will pass
      update o1;
 
    }
}

So insert all of the related records in different methods you will get code coverage... The above code is sample one.

All Answers

Arunkumar RArunkumar R
Hi kon,

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.


@isTest
  Private Class UnitTest_AccountNumberIndex{

static testMethod void LCUnitTest(){

      o1.StageName='Negotiation/Review';
      o1.Probability=90; // This will for your first if statement will pass.
      update o1;
  
    }

static testMethod void LCUnitTest1(){

      o1.StageName='Negotiation/Review';
      o1.Probability=20; // This will goes to second statement will pass
      update o1;
 
    }
}

So insert all of the related records in different methods you will get code coverage... The above code is sample one.
This was selected as the best answer
Kon DeleKon Dele
Thanks. Some clarifiaction, How would I test for Line 24
if  (!accMAP.isEmpty())