需要salesforce触发器的测试覆盖等级

时间:2014-03-12 10:20:50

标签: class triggers salesforce apex-code

我需要为后续触发器编写测试类,以便我可以增加测试环境以在salesforce上传包。我不知道如何为触发器编写测试类。

Trigger  AutoActivityCreation on Account(after update)
{

    List<Task> task = new List<Task>();

for (Integer i = 0; i < Trigger.new.size(); i++) {

                       List<String> techs = Trigger.new[i].Toolsberry_new_tech_installs__c.split(';');                    
                       Integer added_tech = techs.size();

                       if(Trigger.new[i].Toolsberry_new_tech_installs__c=='null'){
                           added_tech=0;
                       }

                        List<String> re_techs = Trigger.new[i].Toolsberry_removed_tech_installs__c.split(';');
                       Integer removed_tech = re_techs.size();

                        if(Trigger.new[i].Toolsberry_removed_tech_installs__c=='null'){
                           removed_tech=0;
                       }


task.add(new Task(
                         whatid=Trigger.new[i].Id,
                         OwnerId=Trigger.new[i].OwnerId,
                         Subject='Technologies Added '+added_tech+' , Removed '+removed_tech,
                         Status = 'Completed',
                         ActivityDate = system.today()
                         ) ) ;
 }

 insert task;

}

can anybody help for this how to write test covrage class.

Thanks
Rajendra J.

1 个答案:

答案 0 :(得分:0)

请检查这是否适合您。

@isTest
private class TriggerTestClass {

    static testMethod void autoTasktest() {
        // TO DO: implement unit test

        Account acc = new Account(
        Name = 'Test Account 1',
        Phone = '07123123123',
        Toolsberry_new_tech_installs__c = 'null',
        Toolsberry_removed_tech_installs__c = 'null');
        insert acc;

        acc.BillingCity = 'Test Area 2';
        update acc;


    }
}

并确保在测试方法中添加了包含帐户对象所需值的字段。它会给你最好的报道。