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
hanifa fatimahanifa fatima 

How to write a test class for Update Trigger on account

trigger UpdateType on Account (before insert,before update,after insert) {
if(trigger.isInsert && trigger.isBefore)
{
for(Account a:trigger.new)
{
if(a.industry=='education')
{
a.addError('we dont deal with education industry');
}
}
}
if(trigger.isUpdate)
{
for(Account a:trigger.new)
{
if(a.Type=='Prospect')
{
a.Type='Other';
}
}
}
}
@isTest
public class UpdateTypeTest {
static testmethod void accUpdate()
{
   Account acc= new Account( Name='Example', Industry='Education');
    insert acc;
    acc=[select name, industry, type from account where type=:acc.Type];
    System.assertEquals('Other', acc.Type);
    update acc;
   
    
}
}

Test class for the Trigger

Here the trigger fires whenever we give the type on account as prospect it would update it to 'Other'

My test class is covering only 60% of the code , somehow it is not covering the updated part. Can someone help me out?

Best Answer chosen by hanifa fatima
Steven NsubugaSteven Nsubuga
@isTest
public class UpdateTypeTest {
    static testmethod void accUpdate()
    {
        Account acc= new Account( Name='Example', Industry='Banking');
        insert acc;
        acc=[select name, industry, type from account];
        acc.Type = 'Prospect';
        update acc;
        acc=[select name, industry, type from account];
        System.assertEquals('Other', acc.Type);
        
        Account acc2= new Account( Name='Example', Industry='education');
        insert acc2;
        
    }
}

 

All Answers

PawanKumarPawanKumar
Please try below.

Trigger
---------------


trigger UpdateType on Account (before insert,before update,after insert) {
if(trigger.isInsert && trigger.isBefore)
{
for(Account a:trigger.new)
{
if((a.industry).equalsIgnoreCase('education'))
{
a.addError('we dont deal with education industry');
}
}
}
if(trigger.isUpdate)
{
for(Account a:trigger.new)
{
if(a.Type=='Prospect')
{
a.Type='Other';
}
}
}
}

Test Class
------------------

@isTest
public class UpdateTypeTest {
static testmethod void accUpdate()
{
   Account acc= new Account( Name='Example', Industry='Education');
    insert acc;
    acc=[select name, industry, type from account where type=:acc.Type];
    System.assertEquals('Other', acc.Type);
    
    // to trigger update block
    acc.Name = 'Example1';

    update acc;
   
    
}
}

Please mark it best if it helps you. Thanks.
Steven NsubugaSteven Nsubuga
@isTest
public class UpdateTypeTest {
    static testmethod void accUpdate()
    {
        Account acc= new Account( Name='Example', Industry='Banking');
        insert acc;
        acc=[select name, industry, type from account];
        acc.Type = 'Prospect';
        update acc;
        acc=[select name, industry, type from account];
        System.assertEquals('Other', acc.Type);
        
        Account acc2= new Account( Name='Example', Industry='education');
        insert acc2;
        
    }
}

 
This was selected as the best answer
Akshay_DhimanAkshay_Dhiman
Hi Hanifa,

Just try this code for writing your test class 
 
@isTest
public class UpdateTypeTest {
static testmethod void accUpdate()
{
   Account acc= new Account( Name='Example', Industry='Education');
    insert acc;
    acc=[select name, industry, type from account LIMIT 1];
 acc.Type = 'Prospect';
    update acc;
  System.assertEquals('Other', acc.Type);
   
    
}
}


Thanks 
Akshay
Giancarlo BaileyGiancarlo Bailey
Great, thanks!