+ Start a Discussion
sandeep87sandeep87 

How to test this trigger getting 71% code coverage

trigger myAccountTrigger on Account(before delete, before insert, before update,
after delete, after insert, after update) {
if (Trigger.isBefore) {
if (Trigger.isDelete) {

// In a before delete trigger, the trigger accesses the records that will be

// deleted with the Trigger.old list.

for (Account a : Trigger.old) {
if (a.name != 'okToDelete') {
a.addError('You can\'t delete this record!');
}
}
} else {

// In before insert or before update triggers, the trigger accesses the new records

// with the Trigger.new list.

for (Account a : Trigger.new) {
if (a.name == 'bad') {
a.name.addError('Bad name');
}
}
if (Trigger.isInsert) {
for (Account a : Trigger.new) {
System.assertEquals('xxx', a.accountNumber);
System.assertEquals('Banking', a.industry);
System.assertEquals(100, a.numberofemployees);
System.assertEquals(100.0, a.annualrevenue);
a.accountNumber = 'yyy';
}

// If the trigger is not a before trigger, it must be an after trigger.

} else {
if (Trigger.isInsert) {
List<Contact> contacts = new Contact[0];
for (Account a : Trigger.new) {
if(a.Name == 'makeContact') {
contacts.add(new Contact (LastName = a.Name,
AccountId = a.Id));
}
}
insert contacts;
}
}
}}}

 

TEST CLASS

@istest
private class myAccountTrigger
{
static testmethod void test()
{
account a=new account(name='makeContact',accountnumber='xxx',industry='banking',numberofemployees=100,annualrevenue=100);
insert a;

system.assertequals(a.accountNumber,'xxx');

account k=new account(name='bad');
try{
insert k;
System.assert(false);
}
catch (Exception e)
{

}

system.assertnotequals('okToDelete',a.name);
delete a ;                // AND HERE IT IS GIVING ERROR 

 

ERROR IS System.DmlException: Delete failed. First exception on row 0 with id 0019000000E4eDUAAZ; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You can't delete this record!: []
STACK TRACE:Class.myAccountTrigger.test: line 22, column 1


}
}

 

 

GETTING 71 % TEST COVERAGE.. ANY HELP WILL BE APPRECIATED

Best Answer chosen by Admin (Salesforce Developers) 
jd123jd123

Hi sandeeep

 

 

     Please highlight what lines are not covered then I can help out you???