+ Start a Discussion
Eric BlaxtonEric Blaxton 

Help to increase test coverage

Hi and thanks in advance.

I have several trigger and a several tests written.  The code coverage is good, 77% and higher per test, but I want to get them higher.  The tests don't cover my .addError code.  the 2 lines not covered are in bold.

My trigger:
trigger checkAccountCreditStatusisRejected on Opportunity (before insert) {
       //Create list to store data in  
       list<id>AccountidLst=new list<id>(); 
      
       for(Opportunity opp : System.trigger.new)
      { // store all Opportunity Account Id's
       AccountidLst.add(opp.AccountId);
      }
      // Create map that stores the id and credit status of Active Opportunity Account
      map<id,account>accountMap=new map<id,account>([select id,Credit_Status__c from account where id In : AccountidLst]);
      
      for (Opportunity opp : System.trigger.new) {          
          if(accountMap.containskey(opp.AccountId))
            {
            if (accountMap.get(opp.AccountId).Credit_Status__c == 'Rejected')
              {
                opp.addError('WARNING: New Opportunity cannot be created when Account Credit Status = Rejected');

              }// end if 
            }
                           
       }//end for  
        
    }// end trigger

My test:
@isTest 
public class testCheckAccountCreditStatusisRejected{
   static testMethod void checkAccountCreditStatusisRejected () {
 
         // create mock user
User mockUser = new User(alias = 'newUser', email='newuser@pbsnow.com',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US', localesidkey='en_US', profileid = UserInfo.getProfileId(),
timezonesidkey='America/Los_Angeles', CommunityNickname='test1', username='testAddDefaultTeam@pbsnow.com');
insert mockUser;  
        
        //Define list
        list<id>AccountidLst=new list<id>();  
        
       //create Account and Opportunity to add default team member
        Account acc = new Account ( Name = 'testAccount', BillingStreet = '123 Main Street', BillingCity = 'Dallas' ,
         BILLINGCOUNTRYCODE='US', BillingStateCode = 'TX', Industry = 'Healthcare', Company__c = 'Pinnacle Business Systems', BillingPostalCode = '75749' );
         insert acc;                     
      
       // store all Opportunity Account Id's
       AccountidLst.add('001J000001DYN5I');     
        
        // Create map that stores the id and credit status of Active Opportunity Account
      map<id,account>accountMap=new map<id,account>([select id,Credit_Status__c from account where Name = 'testAccount']);      
     
       
        Opportunity opp = new Opportunity( Name = 'testOpportunity', Account = acc,/* ToBeSync__c = false,*/ StageName ='Prospecting', Is_this_a_Maintenance_Renewal_Op__c = 'No',Description = 'Test', CloseDate = Date.today(),
         OwnerId = mockUser.id);
                 
        try{
            insert opp;
         
         if(accountMap.containskey('001J000001DYN5I'))
            {
            if (accountMap.get('001J000001DYN5I').Credit_Status__c == 'Rejected')
              {
                opp.addError('WARNING: New Opportunity cannot be created when Account Credit Status = Rejected');
              }// end if 
            }
            
         }//end try
         catch (ListException e) {
         
         }
       
    }

}// end public class

 
Best Answer chosen by Eric Blaxton
Eric BlaxtonEric Blaxton
To solve it, what I did was make 2 changes.  Changes are in bold
1.  //create Account and Opportunity to add default team member
        Account acc = new Account ( Name = 'testAccount', BillingStreet = '123 Main Street', BillingCity = 'Dallas' ,
         BILLINGCOUNTRYCODE='US', BillingStateCode = 'TX', Industry = 'Healthcare', Company__c = 'Pinnacle Business Systems', BillingPostalCode = '75749', Credit_Status__c = 'Rejected' );
         insert acc;    
2. Opportunity opp = new Opportunity( Name = 'testOpportunity', AccountID = acc.id,/* ToBeSync__c = false,*/ StageName ='Prospecting', Is_this_a_Maintenance_Renewal_Op__c = 'No',Description = 'Test', CloseDate = Date.today(),
         OwnerId = mockUser.id);
 

All Answers

Jason Carrigan 9Jason Carrigan 9
Hi Eric, you don't need to explicitly add an error to your opportunity in the test class. You just need to try to create an opportunity with an account where credit status = 'Rejected' and the error will be added by the trigger. You can catch the error in your test class and test for it like this:
 
...

//create Account and Opportunity to add default team member
//set account credit status to rejected
Account acc = new Account ( Name = 'testAccount', Credit_Status__c = 'Rejected', BillingStreet = '123 Main Street', BillingCity = 'Dallas' , BILLINGCOUNTRYCODE='US', BillingStateCode = 'TX', Industry = 'Healthcare', Company__c = 'Pinnacle Business Systems', BillingPostalCode = '75749' );
insert acc;

//create the opportunity
 Opportunity opp = new Opportunity( Name = 'testOpportunity', Account = acc,/* ToBeSync__c = false,*/ StageName ='Prospecting', Is_this_a_Maintenance_Renewal_Op__c = 'No',Description = 'Test', CloseDate = Date.today(),
         OwnerId = mockUser.id);

//insert the opportunity and catch the error
Boolean isError = false;
try{
    
    insert opp;
            
} catch (Exception e) {
     isError = true;
}
       
System.assert(isError, 'Error was caught: credit status invalid');


...



 
Eric BlaxtonEric Blaxton
To solve it, what I did was make 2 changes.  Changes are in bold
1.  //create Account and Opportunity to add default team member
        Account acc = new Account ( Name = 'testAccount', BillingStreet = '123 Main Street', BillingCity = 'Dallas' ,
         BILLINGCOUNTRYCODE='US', BillingStateCode = 'TX', Industry = 'Healthcare', Company__c = 'Pinnacle Business Systems', BillingPostalCode = '75749', Credit_Status__c = 'Rejected' );
         insert acc;    
2. Opportunity opp = new Opportunity( Name = 'testOpportunity', AccountID = acc.id,/* ToBeSync__c = false,*/ StageName ='Prospecting', Is_this_a_Maintenance_Renewal_Op__c = 'No',Description = 'Test', CloseDate = Date.today(),
         OwnerId = mockUser.id);
 
This was selected as the best answer