You need to sign in to do that
Don't have an account?
Eric 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
}
}
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
}
}
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
Let us know if this will help you
All Answers
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/
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
Let us know if this will help you