Reputation: 328745
I don't really understand the behaviour of the tests below. Looking at it, test_OK
and test_Not_OK
are strictly equivalent - the only difference is that test_OK
has "inlined" callMethod
.
However, test_OK
passes whereas test_Not_OK
fails. Is there a reason for that behaviour?
public class MethodCallTest {
@Test
public void test_Not_OK() {
new NonStrictExpectations() {
Whatever w;
{
callMethod();
}
private void callMethod() {
w.method();
result = 1;
}
};
assertEquals(new Whatever().method(), 1); //fails
}
@Test
public void test_OK() {
new NonStrictExpectations() {
Whatever w;
{
w.method();
result = 1;
}
};
assertEquals(new Whatever().method(), 1); //passes
}
public static class Whatever {
public int method() {
return 0;
}
}
}
Upvotes: 2
Views: 135
Reputation: 16390
Good question. The reason is that JMockit performs certain transformations to the bytecode in constructors and initialization blocks of an Expectations
or NonStrictExpectations
subclass. (Essentially, this is done so that the mocking API works. For example, every assignment to the special result
field is actually replaced with a method call, so that it gets properly associated with the current expectation.)
Methods in an expectation block, however, are not yet transformed. They should be, so I am implementing it now. From the next release (0.999.19) on, this will be supported.
Upvotes: 2