Is it possible to leverage both EasyMockSupport
and EasyMockRule
when using EasyMockSupport
through delegation (instead of inheritance)?
In other words: how to make EasyMockSupport
aware of mocks created by EasyMockRule
?
Here is a MWE demonstrating the issue I'm facing:
// Class under test
public class MyClass {
private Collaborator collaborator;
public MyClass() {
collaborator = new Collaborator();
}
// Method under test
public int myMethod() {
return collaborator.mockedMethod() + 1;
}
}
// Class to be mocked
public class Collaborator {
public int mockedMethod() {
return 1;
}
}
// Test case
public class MyClassTest {
private EasyMockSupport easyMockSupport = new EasyMockSupport();
@Rule public EasyMockRule easyMockRule = new EasyMockRule(this);
@TestSubject private MyClass testSubject = new MyClass();
@Mock private Collaborator collaboratorMock;
@Test public void testMyMethod() {
EasyMock.expect(collaboratorMock.mockedMethod()).andReturn(2);
easyMockSupport.replayAll();
int result = testSubject.myMethod();
Assert.assertEquals("Should return 2+1 when successfully mocked", 3, result);
// throws java.lang.AssertionError: expected: <3> but was: <1>
}
}
The test fails, whereas it passes if MyClassTest
extends EasyMockSupport
. (But I can't use inheritance for what I'm doing, hence my question.)
My comprehension of this behavior is that, in my example, EasyMockSupport
isn't aware of the Collaborator
mock, so calling replayAll()
has no effect and the mock is still in record state when called in testSubject.myMethod()
(thus mockedMethod()
returning 0). Indeed, injectMocks()
documentation says:
If the parameter extends EasyMockSupport, the mocks will be created using it to allow replayAll/verifyAll to work afterwards
But when using delegation, the parameter (i.e. the test class) doesn't extend EasyMockSupport
. Is there something I'm missing or is it impossible?
Side note: I'm using EasyMock 3.6. Ideally I'd like to find a solution keeping that version, but feel free to indicate if there is a related feature/bugfix in a later release.
Thanks in advance for your help!
Aucun commentaire:
Enregistrer un commentaire