You need to sign in to do that
Don't have an account?

How to write test class for trigger?
trigger conlists on Account (before insert) {
if(trigger.isInsert && trigger.isAfter)
{
List<Contact> lstCon = new List<Contact>();
for(Account acc : trigger.new)
{
Contact con = new Contact();
con.lastName=acc.Name;
con.AccountId = acc.Id;
lstCon.add(con);
}
insert(lstCon);
}
if(trigger.isUpdate && trigger.isAfter)
{
Map<Id,Account> mapACC = new Map<Id,Account>([select (select id,name from contacts)
from account where id IN: trigger.new]);
List<Contact> newCon = new List<Contact>();
for(Account updatedAcc : trigger.New)
{
Account oldAcc = trigger.oldMap.get(updatedAcc.Id);
if(mapACC.ContainsKEY(updatedAcc.Id) && oldAcc.Name != updatedAcc.Name)
{
for(Contact con : mapACC.get(updatedAcc.Id).contacts)
{
con.lastname = updatedAcc.Name;
newCon.add(con);
}
}
}
update newCon;
}
}
if(trigger.isInsert && trigger.isAfter)
{
List<Contact> lstCon = new List<Contact>();
for(Account acc : trigger.new)
{
Contact con = new Contact();
con.lastName=acc.Name;
con.AccountId = acc.Id;
lstCon.add(con);
}
insert(lstCon);
}
if(trigger.isUpdate && trigger.isAfter)
{
Map<Id,Account> mapACC = new Map<Id,Account>([select (select id,name from contacts)
from account where id IN: trigger.new]);
List<Contact> newCon = new List<Contact>();
for(Account updatedAcc : trigger.New)
{
Account oldAcc = trigger.oldMap.get(updatedAcc.Id);
if(mapACC.ContainsKEY(updatedAcc.Id) && oldAcc.Name != updatedAcc.Name)
{
for(Contact con : mapACC.get(updatedAcc.Id).contacts)
{
con.lastname = updatedAcc.Name;
newCon.add(con);
}
}
}
update newCon;
}
}
Greetings to you!
You can write test class as below :
@isTest public class AccountTriggerTest {
@isTest public static void testInsertAccount(){
Test.startTest();
Account acNewOne = new Account(Name = 'Test');
insert acNewOne;
Contact con = [SELECT Id, LastName FROM Contact WHERE AccountId =: acNewOne.Id];
System.assertNotEqual(null, con);
System.assertEqual('Test', con.LastName);
Test.stopTest();
}
@isTest public static void testUpdateAccount(){
Test.startTest();
Account acNewOne = new Account(Name = 'Test');
insert acNewOne;
acNewOne.Name = 'Test2';
update acNewOne;
Contact con = [SELECT Id, LastName FROM Contact WHERE AccountId =: acNewOne.Id];
System.assertNotEqual(null, con);
System.assertEqual('Test2', con.LastName);
Test.stopTest();
}
}
One more thing in your code, I am not sure that your code will work. Since :
1) in trigger definition, you are using parameter as 'before insert'. So trigger will be executed for before insert.
2) in trigger code, you are using 'if blocks' for Trigger Context Variable 'trigger.isAfter' or/and 'trigger.isUpdate'. So if block will be executed if trigger is called on any Account Update or AFTER event.
Please check it. Rest test class will be working.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Manish Arvind
All Answers
Greetings to you!
As you are using after insert and after update in your code, change your trigger to:
Below is the test class with 100% code coverage:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
Your trigger has some mistakes, so follow the following trigger and its test class:
Trigger: Test-Class:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Greetings to you!
You can write test class as below :
@isTest public class AccountTriggerTest {
@isTest public static void testInsertAccount(){
Test.startTest();
Account acNewOne = new Account(Name = 'Test');
insert acNewOne;
Contact con = [SELECT Id, LastName FROM Contact WHERE AccountId =: acNewOne.Id];
System.assertNotEqual(null, con);
System.assertEqual('Test', con.LastName);
Test.stopTest();
}
@isTest public static void testUpdateAccount(){
Test.startTest();
Account acNewOne = new Account(Name = 'Test');
insert acNewOne;
acNewOne.Name = 'Test2';
update acNewOne;
Contact con = [SELECT Id, LastName FROM Contact WHERE AccountId =: acNewOne.Id];
System.assertNotEqual(null, con);
System.assertEqual('Test2', con.LastName);
Test.stopTest();
}
}
One more thing in your code, I am not sure that your code will work. Since :
1) in trigger definition, you are using parameter as 'before insert'. So trigger will be executed for before insert.
2) in trigger code, you are using 'if blocks' for Trigger Context Variable 'trigger.isAfter' or/and 'trigger.isUpdate'. So if block will be executed if trigger is called on any Account Update or AFTER event.
Please check it. Rest test class will be working.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Manish Arvind