You need to sign in to do that
Don't have an account?
Mark Kh09
Problem getting test coverage for simple trigger
This is my first trigger in the development environment and I wanted to practice getting it test coverage. I know there is a very basic oversight on my part and would like for anyone to comment back on what I'm doing wrong. Rating and Number of Employees is not getting code coverage even though I do an assert for both values.
Here is my trigger:
//-----------------------------------------------------------------------------------
trigger TEST on Account (before insert,before update) {
for (Account accountInLoop : Trigger.new) {
// if the account name is Yippie, then change rating to hot and num emps to 8
if (accountInLoop.Name=='Yippie'){
accountInLoop.Rating = 'Hot';
accountInLoop.NumberofEmployees = 8;
}
}
}
//--------------------------------------------------------
Here is my test:
//---------------------------------------------------------
@isTest
public class TestAccountYippieTrigger {
public static void insertNewAccount(){
Account acctToCreate = new Account();
//start adding fields for new accounts
acctToCreate.Name = 'Yippie';
//insert first new account
insert acctToCreate;
//query inserted account and validate results
Account acctToAssert = [select Id,NumberOfEmployees,Rating from Account where Id=:acctToCreate.Id];
// validate results
System.assertEquals(8, acctToAssert.NumberOfEmployees);
System.assertEquals('Hot', acctToAssert.Rating);
}
}
Here is my trigger:
//-----------------------------------------------------------------------------------
trigger TEST on Account (before insert,before update) {
for (Account accountInLoop : Trigger.new) {
// if the account name is Yippie, then change rating to hot and num emps to 8
if (accountInLoop.Name=='Yippie'){
accountInLoop.Rating = 'Hot';
accountInLoop.NumberofEmployees = 8;
}
}
}
//--------------------------------------------------------
Here is my test:
//---------------------------------------------------------
@isTest
public class TestAccountYippieTrigger {
public static void insertNewAccount(){
Account acctToCreate = new Account();
//start adding fields for new accounts
acctToCreate.Name = 'Yippie';
//insert first new account
insert acctToCreate;
//query inserted account and validate results
Account acctToAssert = [select Id,NumberOfEmployees,Rating from Account where Id=:acctToCreate.Id];
// validate results
System.assertEquals(8, acctToAssert.NumberOfEmployees);
System.assertEquals('Hot', acctToAssert.Rating);
}
}
All Answers