You need to sign in to do that
Don't have an account?
hanifa 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?
All Answers
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.
Just try this code for writing your test class
Thanks
Akshay