Closed brianruss closed 9 years ago
Thanks, that's was a very good problem report!
What happens here is that dynamically generated classes are intentionally excluded from being mocked by a @Capturing mock field. And in this case, a "sun.com.proxy.$Proxy8" is generated at runtime; this is a proxy class which implements ITestObject; it has a null class loader and a null protection domain. JMockit automatically excludes all such classes from consideration, to avoid potential cases of unintended mocking. Note the generated proxy class does not extend "TestObject", which would get mocked if it was eventually loaded during the test; but it's not.
So, this is a limitation of the @Capturing feature; it only mocks "normal" classes that get loaded from a ".class" file. The limitation could be easily eliminated, but doing so could bring unforeseen consequences.
I will do some experiments to see if dropping the restriction on capturing dynamic proxy classes causes problems or not.
I have encountered an issue when using @Capturing. It is related to Spring,Apache CXF, the version of JMockit and also the sequence of running tests. The issue is very obscure, but I have a concise set of files available that is a full maven project that demonstrates the problem. I have tried to put these at the end of the issue, but can send these if needed.
The business model is: An interface ITestObject - Containes a single method that helps to identify when the problem arises. A class TestObjectContainer - A java bean that has a single property of type ITestObject A class TestObject that implements ITestOject
The testing performed is: FirstTest - A Junit test class with a single test method that simply loads a spring XML context file first-test.xml and closes the context SecondTest - A Junit test class with a single test method that
The environment is: Windows 7 Java 8 JUnit 4.12 Spring 4.1.6 Release JMockit - 1.6 or 1.16 (we had been using 1.6 and tried 1.16 when problems arose) CXF 3.0.4 Eclipse Luna
Setup A
Scenario A1
Run SecondTest on its own - PASS - confirms the ITestObject is mocked.
Scenario A2
Run FirstTest then SecondTest as a single execution (I selected the package to run) in eclipse (It is hard to know what order eclipse/junit will run the tests) - SecondTest FAILs - it is attempting to connect with the CXF client proxy.
Setup B
As setup A, but instead of first-test.xml having a CXF client proxy, it has a POJO of type TestObject
Scenario B1
Run FirstTest then SecondTest as a single execution in eclipse -PASS
Setup C
Scenario C1
Run SecondTest on its own - FAIL (This passes on JMockit 1.6)
Setup D
Scenario D1
Run SecondTest on its own - PASS
So, trying to summarise the factors involved:
Apologies for the length of post and amount of detail, but hopefully will aid diagnosis.
Many thanks,
Brian.
Files
src/main/java/com/example/ITestObject.java
src/main/java/com/example/TestObject.java
src/main/java/com/example/TestObjectContainer.java
src/test/java/com/example/FirstTest.java
src/test/java/com/example/SecondTest.java
src/test/resources/first-test.xml
src/test/resources/second-test.xml
pom.xml