Something strange but unsure whether it's expected. Please advise.
I have two test methods in a single test class, which declares a mock annotated with @Mock. The mock is an async service and the service is called when the presenter is instantiated by the gwt initializer. What this means is that each test that uses the presenter ends up calling the same async method. This throws off the expected behaviours associated with the mock.
For example, test1 is intended to verify that the service is called when the presenter is instantiated. Works fine on its own:
class ...
@Mock
private SearchServiceAsync rpcService;
@Test
public void testPresenterInitializationWhenScannerServiceAvailable()
{
ScannerStatusDTO mockStatusDTO = EasyMock.createMock( ScannerStatusDTO.class );
rpcService.getScannerStatus( EasyMock.isA( AdminCallback.class ) );
expectServiceAndCallbackOnSuccess( mockStatusDTO );
replay();
new SearchPresenter( eventBus, display );
verify();
}
But add a second test that is intended to test a separate rpc service and the previous test fails:
...
// this is called by instantiating the presenter (using EasyMock and not expectServiceAndCallbackOnSuccess, because that fails differently - explained below)
rpcService.getScannerStatus( EasyMock.isA( AdminCallback.class ) );
EasyMock.expectLastCall().times( 1 );
rpcService.getMatchingFiles( EasyMock.anyObject( String.class ), EasyMock.anyObject( AsyncCallback.class ) );
expectServiceAndCallbackOnSuccess( expected );
replay();
SearchPresenter searchPresenter = new SearchPresenter( eventBus, display ); // invokes rpcservice.getScannerStatus()
searchPresenter.bind();
Button searchButton = searchPresenter.getSearchButton();
Browser.click( searchButton );
verify();
...
If I run both of these tests in a single test runner, I get a failure on the mock's expectations:
java.lang.AssertionError:
Unexpected method call SearchServiceAsync.getScannerStatus(com.comdev.da.searchportal.client.presenter.AdminCallback@5776dc11):
SearchServiceAsync.getScannerStatus(isA(com.comdev.da.searchportal.client.presenter.AdminCallback)): expected: 1, actual: 2
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44)
The count for each test should be 1, yet the second test holds a reference to what must be a singleton mock, which records two calls against that service. I have tried resetting the mock in the setUp() method and it doesn't do any good. So, it seems that if a test class has multiple test methods that call the same endpoint, those tests have to be broken up into separate test runners.
By the way, if I change the second test to use the GwtEasyMock wrapper around the expectation, i.e., expectServiceAndCallbackOnSuccess( new ScannerStatusDTO() ), the second test throws an UmbrellaException: Exception caught: null