部署运行测试通过,但仍显示所选Apex触发器的测试覆盖率为0%,至少为1%
Apex触发器:
部署失败:消息:“所选Apex触发器的测试覆盖率为0%,至少需要1%的测试覆盖率”
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;
}
Apex class - test and pass
@isTest
private class AutoActivityCreationTest
{
static testMethod void myUnitTest()
{
List<Task> task = new List<Task>();
Account testAccount = new Account(name='Test Company Name',Toolsberry_new_tech_installs__c='',Toolsberry_removed_tech_installs__c='');
insert testAccount;
List<String> techs = testAccount.Toolsberry_new_tech_installs__c.split(';');
Integer added_tech = techs.size();
if(testAccount.Toolsberry_new_tech_installs__c=='null'){
added_tech=0;
}
List<String> re_techs = testAccount.Toolsberry_removed_tech_installs__c.split(';');
Integer removed_tech = re_techs.size();
if(testAccount.Toolsberry_removed_tech_installs__c=='null'){
removed_tech=0;
}
Task testTask = new Task(WhatID = testAccount.Id,Subject= 'Technologies Added '+added_tech+' , Removed '+removed_tech, Status = 'Completed',
ActivityDate = system.today());
test.startTest();
insert testTask;
System.assertEquals('Technologies Added '+added_tech+' , Removed '+removed_tech,testtask.Subject);
test.stopTest();
}
}
答案 0 :(得分:2)
您的触发器是“更新后”,但我在测试代码中看到的只是“插入testTask”。
您需要修改触发器以包含“插入后”或调整测试,以便它还对对象“testTask”进行更新。一旦执行了这些选项中的任何一个,就会从测试代码中调用触发器代码。