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
WalidWalid 

AssertEquals fails when testing!

Hi there,

I am testing a trigger with its test class:
trigger ownerManager on Opportunity (before insert, before update) {
	/*
	Write a trigger that populates an “Owner’s Manager” lookup field on opportunities 
	based on the opp owner’s (user’s) standard ManagerId field. 
	*/
	
	Set<Id> allUsersID = new Set<Id>();
	for (Opportunity so : Trigger.new) {
		//for each opportunity, get the owner's manager
		allUsersID.add(so.OwnerId);
	}

	List<User> users = new List<User>();
	users = [SELECT Id, ManagerId
			   FROM User
			  WHERE Id IN :allUsersID];

	Map<Id,Id> userToManager = new Map<Id,Id>();
	for (User us : users) {
		userToManager.put(us.Id,us.ManagerId);
	}

	for (Opportunity so : Trigger.new) {
		//for each opportunity, set the Owner's Manager Id to be the Owner's Manager from the Map
		so.Owner_s_Manager__c = userToManager.get(so.OwnerId);
	}
}
Test Class:
@isTest
private class ownerManagerTest {
	
	@isTest static void test_method_one() {
		// create a user, and populate his manager
		Profile myProfileId = [SELECT id
							     FROM profile
							    WHERE name = 'Standard User'
							    LIMIT 1];
			
		User u = new User(Alias = 'standt', Email='walidtestusers@walidtestorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = myProfileId.Id, ManagerId='0050Y000000Fm7z',
            TimeZoneSidKey='America/Los_Angeles', UserName='walidtestusers@walidtestorg.com');

		insert u;
		System.debug('1 No Opp yet, but User Manager ID: ' +u.ManagerId);

		Opportunity opp = new Opportunity (Name='Test Opp', CloseDate=date.TODAY(), 
			StageName='Prospecting', ownerID=u.Id);

		Insert opp;
		System.debug('2: OPP ownerID = ' +opp.ownerID +' should be = ' +u.Id 
						+' OPP ManagerId = ' + opp.Owner_s_Manager__c);

		System.assertEquals(u.ManagerId,opp.Owner_s_Manager__c, 'Not the same!');
	}
}

Manual testing succeeds, i.e, I when I create an opportunity from the UI, the field gets populated qithout any issue. But when performing the Unit Test, the assertion fails with the message

"System.AssertException: Assertion Failed: Not the same!: Expected: 0050Y000000Fm7zQAC, Actual: null
Stack Trace 
Class.ownerManagerTest.test_method_one: line 26, column 1"

What could be the issue?

Thanks.
Walid
Best Answer chosen by Walid
V V Satyanarayana MaddipatiV V Satyanarayana Maddipati
Hello

In the above code, you are using the same instance to check the Owner_s_Manager__c field, which is null.

Trigger operation will happen in Database level. To access the updated values in your code, Query the object with necessary fields and do Assertion as shown below:
opp = [select Owner_s_Manager__c from Opportunity where id =: opp.Id limit 1];
System.assertEquals(u.ManagerId,opp.Owner_s_Manager__c, 'Not the same!');
Hope this helps.

Best Regards,
Satyanarayana.

All Answers

V V Satyanarayana MaddipatiV V Satyanarayana Maddipati
Hello

In the above code, you are using the same instance to check the Owner_s_Manager__c field, which is null.

Trigger operation will happen in Database level. To access the updated values in your code, Query the object with necessary fields and do Assertion as shown below:
opp = [select Owner_s_Manager__c from Opportunity where id =: opp.Id limit 1];
System.assertEquals(u.ManagerId,opp.Owner_s_Manager__c, 'Not the same!');
Hope this helps.

Best Regards,
Satyanarayana.
This was selected as the best answer
WalidWalid
Totally forgot about that! Thanks mate! :)