• cstark
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

We have run into an issue when running unit tests that call a class from a managed package using a mock. When doing data manipulation within a test method (inserting new records) calling the class returns null value (in the case below a string 'Success' is expected to return).

@isTest(SeeAllData=true)
	static void testMock1() {
		system.debug(ManagedPackage1.Class1.ReturnSuccess('result', new ManagedPackage1.MockGenerator.Result1());
	}


Running the test above, the debug of 'Success' returns as expected. However, if any data manipulation is done at the beginning of the test method, null is always returned.
 

@isTest(SeeAllData=true)
	static void testMock1() {
		Account a = new Account();
		insert a;
		system.debug(ManagedPackage1.Class1.ReturnSuccess('result', new ManagedPackage1.MockGenerator.Result1());
	}

The bahavior completely changes, and the debug returns null. We have found a way around it doing the following.

@isTest(SeeAllData=true)
	static void testMock1() {
		Account a = new Account();
		insert a;
		test.starttest();
		system.debug(ManagedPackage1.Class1.ReturnSuccess('result', new ManagedPackage1.MockGenerator.Result1());
		test.stoptest();
	}
The result returns 'Success' as intended wrapped in test.starttest() and test.stoptest(). If the insert of the Account is also done within the start and stop test, the debug is again null. Any ideas as to why this behaves the way it does?