1

Привет может кто-нибудь помочь мне написать TestClass для контакта триггера: изо все сил, чтобы получить высокий уровень охвата кода:Как writeApex тестовый класс для Trigger контакта и так далее для других триггеров счетов и связаться с

trigger Contact on Contact (before insert, before update) {  


    private String TRIGGERNAME = 'Contact';  

    private String sourceOfError = ' Trigger : '+ TRIGGERNAME ;  


    try { 
     ContactHelper.copyAddressFields(Trigger.new);  

    }  
    catch(Exception ex){  
     // Adding error on all contact records, preventing write  
     for(Contact con : Trigger.new){ 
     con.addError(ex.getMessage()); 
     } 
      Utils.addErrorInLogForException(ex, sourceOfError); 
     } 
} 

ответ

0

Он будет быть сложным дать полный ответ здесь, поскольку это будет зависеть от того, что делает вызов метода ContactHelper.copyAddressFields(Trigger.new);.

В общем:

@IsTest 
public class ContactTrigger_Test { 

    public testMethod static void contactInsertion() { 
     Account acct = new Account(name='test account'); 
     insert acct; 

     // Consider setting some address fields as required by copyAddressFields 
     Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex'); 
     insert c; 

     //Make some assertions based on the results of the copyAddressFields call 
    } 

    public testMethod static void contactInsertionFails() { 
     Account acct = new Account(name='test account'); 
     insert acct; 

     // Set some fields that will cause an Exception in copyAddressFields 
     Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex'); 
     insert c; 

     //Make some assertions that errors were added to the Contact 
    } 

    public testMethod static void bulkifedInsertaion() { 
     Account acct = new Account(name='test account'); 
     insert acct; 

     Contact[] contactsToCreate = new Contact[]{}; 

     for(Integer x=0; x<200;x++){ 
      Contact ct = new Contact(AccountId=acct.Id,lastname='testing',firstname='apex'); 
      contactsToCreate.add(ct); 
     } 

     Test.startTest(); 
     insert contactsToCreate; 
     Test.stopTest(); 
    } 
} 

Кроме того, есть Распознать An Introduction to Apex Code Test Methods