-
Notifications
You must be signed in to change notification settings - Fork 239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cascading Mocks for different matches of the 'base' mock #153
Comments
I've changed my test to use two instances of baseMock. Unfortunately with the same result. The 'generated' cascading mock does not differentiate instances. @Test
public void cascadingMockTest(@Injectable BaseMock baseMock1, @Injectable BaseMock baseMock2) {
new Expectations() {{
baseMock1.getCascadingMock("John").getString(); returns("John1", "John2");
baseMock2.getCascadingMock("Hugo").getString(); returns("Hugo1", "Hugo2");
}};
assertEquals("John1", baseMock1.getCascadingMock("John").getString());
assertEquals("Hugo1", baseMock2.getCascadingMock("Hugo").getString());
assertEquals("John2", baseMock1.getCascadingMock("John").getString());
assertEquals("Hugo2", baseMock2.getCascadingMock("Hugo").getString());
} |
I will see if it's possible (ie, without breaking existing tests) to create new cascaded instances for recorded expectations, instead of reusing existing ones. For now, one has to declare separate mocks for the intermediate objects and explicitly record them. For example: @Test
public void cascadingMockTest(
@Mocked BaseMock baseMock,
@Mocked CascadingMock john, @Mocked CascadingMock hugo
) {
new Expectations() {{
baseMock.getCascadingMock("John"); result = john;
john.getString(); returns("John1", "John2");
baseMock.getCascadingMock("Hugo"); result = hugo;
hugo.getString(); returns("Hugo1", "Hugo2");
}};
assertEquals("John1", baseMock.getCascadingMock("John").getString());
assertEquals("Hugo1", baseMock.getCascadingMock("Hugo").getString());
assertEquals("John2", baseMock.getCascadingMock("John").getString());
assertEquals("Hugo2", baseMock.getCascadingMock("Hugo").getString());
} |
Hello Rogerio, thx for your answer. I'm aware that it works with separate mocks for the intermediate objects, bit IMO are a quite unique feature of jmockit to write concise tests. If there is a way I would be quite happy :-) Jürgen |
As it turns out, this is in fact a bug. It only happens when the |
I have the following code. Already the first assert fails because jmockit can't differentiate the cascading mocks due to parameters in the base mock. It would be a big help if this would be possible.
The text was updated successfully, but these errors were encountered: