I have written a simple trigger which does some validation. I am trying to understand, if it's really necessary to write Test Class for this. The code coverage is 0%, hence please someone help to understand how to write Test Class for the below code.
trigger Duplicate_Prospect_Check on Account (before insert)
{
list <Account> Acc = [Select id from Account where Account.name=:trigger.new[0].name and Account.Siebel_Account_ID__c=:trigger.new[0].Siebel_Account_ID__c];
if(Acc.size()>0)
{
trigger.new[0].name.addError('Prospect Account with same Name and Account Id already exists');
}
}
So I added below code in my Test Class and it ran successfully. But my actual trigger is still not giving 100% code coverage. It's not covering 'addError' statement.
private class Duplicate_Prospect_Check_TriggerTest
{
static testmethod void test()
{
List<Account> acts = new List<Account>();
for(Integer i=0; i<2; i++)
{
Account a = new Account(Name='Duptest',ShippingCity='Boston',ShippingCountry='United States',ShippingState='MA');
acts.add(a);
}
try
{
test.startTest();
insert acts;
test.stopTest();
}
catch(Exception e)
{
System.Assert(e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'));
}
}
}