Reputation: 438
I have a code that will be tested:
public void ackAlert(final Long alertId, final String comment) {
final AnyTask task = AnyTask.create(
"ackAlert", new Class[] { Long.class, String.class },
new Object[] { alertId, comment });
taskExecutor.execute(task);
}
I'm writting test to it:
public void testAckAlert() throws Exception {
final Long alertId = 1L;
final String comment = "tested";
final AnyTask task = AnyTask.create(
"ackAlert", new Class[] { Long.class, String.class },
new Object[] { alertId, comment });
taskExecutor.execute(task);
expectLastCall();
replay(taskExecutor);
testingObjectInstance.ackAlert(alertId, comment);
verify(taskExecutor);
}
And I got exception:
java.lang.AssertionError: Unexpected method call execute(com.alert.bundle.model.AnyTask@4cbfea1d): execute(com.alert.bundle.model.AnyTask@65b4fad5): expected: 1, actual: 0
Where is my error? I think problem is in invocation of static method create.
Upvotes: 1
Views: 1931
Reputation: 5971
It may not be important to mock your static method, depending on what it is that you want to test. The error is because it does not see the task that gets created in the method you are testing as equal to the task you passed to the mock.
You could implement equals and hashCode on AnyTask so that that they do look equivalent. You could also 'capture' the task being passed to execute and verify something about it after the test. That would look like this:
public void testAckAlert() throws Exception {
final Long alertId = 1L;
final String comment = "tested";
mockStatic(AnyTask.class);
Capture<AnyTask> capturedTask = new Capture<AnyTask>();
taskExecutor.execute(capture(capturedTask));
expectLastCall();
replay(taskExecutor);
testingObjectInstance.ackAlert(alertId, comment);
AnyTask actualTask = capturedTask.getValue();
assertEquals(actualTask.getName(), "ackAlert");
verify(taskExecutor);
}
If you are not really testing anything about the task, but just that the taskExecutor.execute()
is called, you could simply replace
taskExecutor.execute(task);
with
taskExecutor.execute(isA(AnyTask.class));
or even
taskExecutor.execute(anyObject(AnyTask.class));
Upvotes: 1
Reputation: 5971
I don't see where you're creating your mocks, but yes, mocking a static method call can't be done with EasyMock alone. However, PowerMock can be used with either EasyMock or Mockito to mock a static method call.
You will need to annotate your test class with @RunWith(PowerMockRunner.class)
and @PrepareForTest(AnyTask.class)
. Then your test would look something like this:
public void testAckAlert() throws Exception {
final Long alertId = 1L;
final String comment = "tested";
mockStatic(AnyTask.class);
final AnyTask task = new AnyTask();
expect(AnyTask.create(
"ackAlert", new Class[] { Long.class, String.class },
new Object[] { alertId, comment })).andReturn(task);
taskExecutor.execute(task);
expectLastCall();
replay(AnyTask.class, taskExecutor);
testingObjectInstance.ackAlert(alertId, comment);
verify(taskExecutor);
}
Upvotes: 0