Closed marcfarrow closed 9 years ago
Thanks for reporting; seems the second class implemeting the interface is not getting mocked. A simpler version of the test follows.
public interface TestInterface { String getData(); }
static final TestInterface INTER1 = new TestInterface() { @Override public String getData() { return "1"; } };
static final TestInterface INTER2 = new TestInterface() { @Override public String getData() { return "2"; } };
public static class MockTestInterface<T extends TestInterface> extends MockUp<T> {
@Mock
public String getData(Invocation invoke) { return "MOCKED DATA: " + invoke.proceed(); }
}
@Test
public void mockAllClassesImplementingAnInterface() {
String test1Value = INTER1.getData();
String test2Value = INTER2.getData();
new MockTestInterface();
String mocked1Value = INTER1.getData();
String mocked2Value = INTER2.getData();
assertNotEquals(test1Value, mocked1Value);
assertNotEquals(test2Value, mocked2Value);
}
We have an internal library in which parts of the library implement a global interface. Sometimes, the implementation of this interface is an anonymous class. We are trying to partially mock all the interface implementations for that interface definition. We want to create these "MockUp"s in an external class for testing purposes so we can manipulate the call to the method to "change" the value returned. We don't just want a new value returned, we want to physically add information to the existing call results. I have created a full testing package that uses jMockit 1.17 and jUnit 1.4 with an attempt to achieve this goal. It is very likely I am missing something small or that I have found a bug. There are two test cases in the unit test. The first case fails because the real value and the mocked value are the same which denotes that the interface implementations are not being mocked. The second case fails with exception. This second case is the real issue where I think there might be a bug. the instantiation of the mocked objects successfully mocks all interfaces. However, when the second implementation runs through the mocked code, an IllegalArgumentException (full stack trace will be provided) is thrown. It is my opinion that the "mocked" code was originally instantiated and loaded. The second time the "mocked" code is supposed to be ran it using the same instantiation which is related to TestClass1$1 instead of TestClass2$1. This can be seen by reviewing the Exception.getMessage() of the of the stack trace (which states TestClass1$1) versus the TestClass2$1 in the stack trace elements. Any thoughts or ideas would be greatly appreciated.
Test1 Results:
Test2 Results:
TestInterface.java
TestClass1.java
TestClass2.java
MockTestInterface1.java
MockTestInterface2.java
JUnitTestCase.java