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
ImmuImmu 

Test Class for Trigger(I'm New to Test Class)

Hi

   Iam New to Test Class...Iam struggling to write Test Class to my Trigger..

   

  Can any one write the test class for below Trigger...

 

  

trigger ownershipTrigger on Account (before update) {
Map<Id,Account> mp=new Map<Id,Account>();
mp=trigger.oldMap;
for(account a:trigger.new)
{
Account al=new Account();
al=mp.get(a.Id);
if(a.Ownership!=al.Ownership)
{
a.ownership.adderror('You Cannot Change this Ownership');
}
}
}

 

Any help would be greatly appreciated..

digamber.prasaddigamber.prasad

Hi,

 

Assuming that 'Name' is only mandatory field on Account, I have written below test class. You can always modify it to get to your business objective.

 

@isTest
private class TestOwnershipTrigger {
	public static testmethod void testTrigger() {
		User u1 = new User(firstName = 'AAA', lastName = 'BBB', email = 'aaabbb@aaabbb.nowhere', Username = System.currentTimeMillis() + 'aaabbb@aaabbb.nowhere.demoxxx', Alias = 'USR', CommunityNickname =System.currentTimeMillis() + 'AAA BBB', EmailEncodingKey='ISO-8859-1', TimeZoneSidKey='Europe/London', LanguageLocaleKey='en_US', LocaleSidKey = 'en_US', ProfileId = p.Id);
		User u2 = new User(firstName = 'CCC', lastName = 'DDD', email = 'cccddd@aaabbb.nowhere', Username = System.currentTimeMillis() + 'cccddd@aaabbb.nowhere.demoxxx', Alias = 'USR', CommunityNickname =System.currentTimeMillis() + 'CCC DDD', EmailEncodingKey='ISO-8859-1', TimeZoneSidKey='Europe/London', LanguageLocaleKey='en_US', LocaleSidKey = 'en_US', ProfileId = p.Id);
		
		List<User> lstUser = new List<User>();
		lstUser.add(u1);
		lstUser.add(u2);
		insert lstUser;
		
		Account acc = new Account(Name = 'Test Account1', OwnerId=lstUser[0].Id);
		insert acc;
		acc.OwnerId = lstUser[1].Id;
		update acc;
		
	}
}

 

Hopefully, it will help you!

 

Happy to help you!

 

Regards,

Digamber Prasad