I wrote a trigger to prevent the deletion of a record. And also I'm writing test class.
public class ClassA
public static void preventDelete(List<Person__c> pers) {
for(Person__c per: pers) {
if(per.hasJob__c == true) {
rra.addError('You cannot delete the record.');
}
}
}
}
On trigger:
trigger PersonTrigger on Person__c (before delete) {
if(trigger.isDelete) {
if (trigger.isBefore){
ClassA.preventDelete(trigger.old);
}
}
}
But I'm thinking that I can write test class for this trigger by calling the method directly e.g: ClassA.preventDelete(myList); or by creating records on test class then check if it was successfully deleted or not. Then what is the difference between the two and which one is better?
Example 1:
@isTest
public class ClassATest {
public static testMethod void TestPreventDelete() {
Person__c p1 = new Person__c(Name='Joseph', hasJob__c=true);
List<Person__c > pers = new List<Person__c>();
pers.add(p1);
insert pers;
ClassA.preventDelete(pers); // test class by calling the method to cover theof testa class
}
}
Example 2:
@isTest
public class ClassATest {
public static testMethod void TestPreventDelete() {
Person__c p1 = new Person__c(Name='Joseph', hasJob__c=true);
insert p1;
// test class by using dml statement
Database.DeleteResult result = Database.delete(p1, false);
System.assert(!result.isSuccess());
}
}