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
Iyappan Kandasamy 8Iyappan Kandasamy 8 

Need test class for the update triggers...and the insert trigger..

Hi...Actually I have written 2 triggers and I need test class for these two triggers...
1. Trigger to update contact description whenever account description is updated with the same value of account description...

trigger acc on account(after update)
{
set<id> accids = new set<id>();
for(account a : trigger.new)
{
accids.add(a.id);
}
list<contact> conlist=[select id,account.description, description from contact where accountid=: accids];
for (contact c: conlist)
{
c.description=c.account.description;
}
update conlist;
}

2.) Whenever account is created then create contact with the account details ...

trigger createcon on account(after insert)
{
list<contact> conlist = new list<contact>();
for (account a:trigger.new)
{
contact c = new contact();
c.lastname= a.name;
c.accountid=a.id;
conlist.add(c);
}
insert conlist;
}


Kindly need test classes for the above 2 triggers. 
Any help on writing the test classes would be of great help....Thanks...
Thanks in advance...
Deepali KulshresthaDeepali Kulshrestha
Hi Iyappan,

- I read your problem and implemented it in my Org and it is working fine.
- Please use the below code (Solved with 100% code coverage)

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

@isTest public class createcon_Test {

        @isTest public static void tset1()
            {
                Test.startTest();


                Account acc=new Account();
                acc.Name='Test';
                insert acc;
                update acc;

                Test.stopTest();
            }

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
iyappan kandasamy 4iyappan kandasamy 4
Hi..Deepa...Thanks for your help on the test class...but I have a doubt...Whether your solution is for both the triggers or for second trigger or whether I can use it for both...please...Appreciate your help....
Andrew GAndrew G
Hi iyappan
You are correct to question the quality of the trigger.  Whilst it provides "coverage" , it provides no assurance that your code is operating correctly.
We can roll the two triggers into one test.  I would just annotated both triggers to describe which test class provides coverage:
/** *This trigger * Test Class - createContact_Test */
Test class below provides coverage and asserts.  (not it has not been compiled).
@isTest public class createContact_Test {

        @isTest public static void fullTest()
            {
                Account acc=new Account();
                acc.Name='Test';
		        acc.description ='New';
                insert acc;
		//get the inserted contacts
		List<contacts> contacts = [SELECT Id, lastName FROM Contact WHERE accountId =:acc.Id];
		//test the number that were created
		System.assertequals(1,contacts.size();
		//test that the name has been set correctly.
		for (Contact con : contacts ) {
			System.assertequals(acc.Name, con.lastname);
		}
		
		//update the acc to ensure the details is passed to the contact record
		acc.description = 'Updated';
		//get the updated contacts
		List<contacts> contacts = [SELECT Id, lastName FROM Contact WHERE accountId =:acc.Id];
		//test that the description has been updated correctly.
		for (Contact con : contacts ) {
			System.assertequals(acc.Description, con.Description);
		//or test the hard coded string
			System.assertequals('Updated', con.Description);
		}
            }
}

Note also, for best practice, you should have one trigger per object.  And the bundle the trigger activities in to a handler class (as best practice).
The reason is that for multiple triggers, if they have the same event, you cannot control the order in which they fire. 

Regards
Andrew
 
Deepali KulshresthaDeepali Kulshrestha
Hi Iyappan,

Yes, it is for both the triggers.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.