+ Start a Discussion
Eric BlaxtonEric Blaxton 

help to improve code coverage

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 stopDuplicateRegRequest on Registration_Requests__c (before insert) {
    Map<String, Registration_Requests__c> regMap = new Map<String, Registration_Requests__c>();
    Map<String, Registration_Requests__c> regMap1 = new Map<String, Registration_Requests__c>();    
    Map<Boolean, Registration_Requests__c> regMap2 = new Map<Boolean, Registration_Requests__c>();
    // Notes:  I don't need this to check before update.  If they update an existing Trigger then it doesn't create a new Reg Req and Reg so no reason for it.
           
    for (Registration_Requests__c regReq : System.Trigger.new) {
                // store registration Request pertinent values
                regmap.put(regReq.Account__c, regReq);
                regmap1.put(regReq.Product__c,regReq);                    
                regmap2.put(regReq.Duplicate_Registration__c,regReq); 
       
    //Loop through and make sure no duplicates exist
    //fields are Account, Product and Status != 'Closed'  
    
    for (Registration__c reg : [SELECT Account__c,Product__c, Registration_Status__c, Duplicate_Registration__c FROM Registration__c])
 {
          If (regReq.Duplicate_Registration__c == False && reg.Registration_Status__c != 'Closed' && reg.Account__c == regReq.Account__c && reg.Product__c == regReq.Product__c )
           {
              regReq.addError('Registration for this Product already exists for this Account. If you want to enter it anyway, check the "Account has multiple locations" checkbox'
                       + ' and fill out the Location field');
             break; 
           }// end If (regReq.Duplicate_Registration__c
                        
 }// end for (Registration__c reg
    } // end for (Registration_Requests__c
} // end trigger

My Test:

@isTest
private class TestStopDupsonRR {

 static testMethod void testPreventDuplicateRegistration() {   
      
     
// Try to insert duplicate Registration Request
      
Registration_Requests__c regReq = new Registration_Requests__c(Account__c='001U000000hWgX5',
  Inside_Sales_Rep_Name__c='005U0000000Tusv', Manufacturer_Name__c = '00NU00000038wXh', Product__c = '01tU0000001JToC', Status__c = 'In Progress', Location__c = 'Test');
 
 insert regReq;      

 // Seed the database with some Regisrations and make sure they can be bulk inserted successfully.
  Registration__c reg = new Registration__c(Account__c='001U000000hWgX5', Inside_Sales_Rep_Name__c='005U0000000Tusv',
  Product__c = '01tU0000001Jo10', Manufacturer_Name__c = '00NU00000038wXh', Location__c = 'Test', Registration_Status__c = 'Closed');  

 //Registration__c[] regs = new Registration__c[] {r1};      
 Try {
   insert reg;
      
       If (regReq.Duplicate_Registration__c == False && reg.Registration_Status__c != 'Closed' && reg.Account__c == regReq.Account__c && reg.Product__c == regReq.Product__c )
           {
              regReq.addError('Registration for this Product already exists for this Account. If you want to enter it anyway, check the "Account has multiple locations" checkbox'
                       + ' and fill out the Location field');
            // break; 

           }// end If (regReq.Duplicate_Registration__c
 
   } //end try
   catch (ListException e){
   
   }// end catch
}
}
Best Answer chosen by Eric Blaxton
Amit Chaudhary 8Amit Chaudhary 8
I found below issue in your test class
1) You are using to much HardCoded ID. Please try to create test data in your test class and add actual id.
2) To Cover the exeption past you need to set below data in your test class
Account acc = new Account();
acc.name ='Test';
insert acc;


Registration__c reg = new Registration__c();
reg.Duplicate_Registration__c =false;
reg.Registration_Status__c ='Open';
reg.Account__c = acc.id; // Please use same account id for Registration_Requests__c as well
// reg.Product__c  = prod.id; // create product here and set same id
// add all required field
insert reg;

// Then create Registration_Requests__c  record

Let us know if this will help you
 

All Answers

pconpcon
You will want to create multiple test methods and each one of them should be testing your specific goal.  For the lines that are missed, you will need to make sure that your regReq and reg records have the data in them that cause you go get into the if statement.

I recommend reading over this page [1] for more information.

NOTE: When including code please use the "Add a code sample" button (icon <>) to increase readability and make referencing code easier.

[1] http://blog.deadlypenguin.com/blog/testing/strategies/
Amit Chaudhary 8Amit Chaudhary 8
I found below issue in your test class
1) You are using to much HardCoded ID. Please try to create test data in your test class and add actual id.
2) To Cover the exeption past you need to set below data in your test class
Account acc = new Account();
acc.name ='Test';
insert acc;


Registration__c reg = new Registration__c();
reg.Duplicate_Registration__c =false;
reg.Registration_Status__c ='Open';
reg.Account__c = acc.id; // Please use same account id for Registration_Requests__c as well
// reg.Product__c  = prod.id; // create product here and set same id
// add all required field
insert reg;

// Then create Registration_Requests__c  record

Let us know if this will help you
 
This was selected as the best answer