0

I've added expectation for a method like this,

expect(locationManager.isLocationSettingsEnabled(anyObject(FragmentActivity.class))).andReturn(isLocationEnabled).anyTimes();

replay(locationManager);

But when I add, expectation for one more method(mentioned below) after replay, the first method is reset automatically.

expect(locationManager.isNotificationSettingsEnabled(anyObject(FragmentActivity.class))).andReturn(isNotificationsEnabled).anyTimes();

I would like to know how to add one more expectation without resetting it.

Sadeshkumar Periyasamy
  • 4,848
  • 1
  • 26
  • 31
  • i think you should use [google mock](http://stackoverflow.com/questions/5743236/google-mock-multiple-expectations-on-same-function-with-different-parameters) or [mockito](http://stackoverflow.com/questions/8088179/using-mockito-with-multiple-calls-to-the-same-method-with-the-same-arguments). – Mehran Zamani Feb 11 '17 at 11:19

1 Answers1

1

Easymock functions on this principle.

  • When You set some expectations on method, you are typically faking/mocking the behaviour of that method.
  • Now when you call replay(mockObject), Easymock injects this mocked behaviour in Test Runner environment.

Therefore, you need to do all the expectations on a mocked object before you replay the mocked object.

something like this:

EasyMock.expect(mockObject.method1()).andReturn(null);
EasyMock.expect(mockObject.method2()).andReturn(null);

EasyMock.replay(mockObject);

Looking closely at your Question, I see that You are mocking a single method with two different return clauses you can do something like this :

EasyMock.expect(mockObject.method1()).andReturn(new Integer(1)).once();
EasyMock.expect(mockObject.method1()).andReturn(new Integer(2)).once();

EasyMock.replay(mockObject);

by this Easymock will return 1 as output for first time when method is invoked and 2 when method is invoked for second time.

Hope this Helps!

Good luck!

Vihar
  • 3,626
  • 2
  • 24
  • 47
  • 1
    As a side note, `once()` isn't required since it's the default. And you can also pipe calls `andReturn(1).andReturn(2)`. – Henri Feb 13 '17 at 19:28