function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mark Kh09Mark 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);
   }
}
Best Answer chosen by Mark Kh09
KaranrajKaranraj
Mark - 'testMethod' is the keyword which you are missing in your test class method, other than that you are code looks fine.
 
@isTest
public class TestAccountYippieTrigger {
    public static testMethod 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

KaranrajKaranraj
Mark - 'testMethod' is the keyword which you are missing in your test class method, other than that you are code looks fine.
 
@isTest
public class TestAccountYippieTrigger {
    public static testMethod 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);
   }
}

 
This was selected as the best answer
Mark Kh09Mark Kh09
Thank you so much!  I'm going to give it a try now.
Mark Kh09Mark Kh09
It works now.  Thank you for taking the time to answer.