It seems PowerMock ist not working with JAXB, or I am doing something completely wrong! When running this example code, the below exception is thrown by JAXB. Can anybody tell a PowerMock novice like me what my fault is? (Dependencies found at the end of this posting)@PrepareForTest(X.class) public final class PowermockTest {
@XmlRootElement public static final class X {
private X() { }
}
@Rule public PowerMockRule USE_POWER_MOCK = new PowerMockRule();
@Test public final void x() {
final X x = PowerMock.createMock(X.class);JAXB.marshal(x, System.out);
}
}javax.xml.bind.DataBindingException: javax.xml.bind.JAXBException
- with linked exception:
[com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
net.sf.cglib.proxy.Callback is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
at net.sf.cglib.proxy.Callback
at public net.sf.cglib.proxy.Callback[] foo.bar.PowermockTest$X$$EnhancerByCGLIB$$f0189e84.getCallbacks()
at foo.bar.PowermockTest$X$$EnhancerByCGLIB$$f0189e84
net.sf.cglib.proxy.Callback does not have a no-arg default constructor.
this problem is related to the following location:
at net.sf.cglib.proxy.Callback
at public net.sf.cglib.proxy.Callback[] foo.bar.PowermockTest$X$$EnhancerByCGLIB$$f0189e84.getCallbacks()
at foo.bar.PowermockTest$X$$EnhancerByCGLIB$$f0189e84
]
at javax.xml.bind.JAXB._marshal(Unknown Source)
at javax.xml.bind.JAXB.marshal(Unknown Source)
at foo.bar.PowermockTest.x(PowermockTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.powermock.modules.junit4.rule.PowerMockStatement$1.run(PowerMockRule.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:2014)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:885)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:713)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:98)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: javax.xml.bind.JAXBException
- with linked exception:
[com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
net.sf.cglib.proxy.Callback is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
at net.sf.cglib.proxy.Callback
at public net.sf.cglib.proxy.Callback[] foo.bar.PowermockTest$X$$EnhancerByCGLIB$$f0189e84.getCallbacks()
at foo.bar.PowermockTest$X$$EnhancerByCGLIB$$f0189e84
net.sf.cglib.proxy.Callback does not have a no-arg default constructor.
this problem is related to the following location:
at net.sf.cglib.proxy.Callback
at public net.sf.cglib.proxy.Callback[] foo.bar.PowermockTest$X$$EnhancerByCGLIB$$f0189e84.getCallbacks()
at foo.bar.PowermockTest$X$$EnhancerByCGLIB$$f0189e84
]
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.find(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXB$Cache.<init>(Unknown Source)
at javax.xml.bind.JAXB.getContext(Unknown Source)
... 38 more
Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
net.sf.cglib.proxy.Callback is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
at net.sf.cglib.proxy.Callback
at public net.sf.cglib.proxy.Callback[] foo.bar.PowermockTest$X$$EnhancerByCGLIB$$f0189e84.getCallbacks()
at foo.bar.PowermockTest$X$$EnhancerByCGLIB$$f0189e84
net.sf.cglib.proxy.Callback does not have a no-arg default constructor.
this problem is related to the following location:
at net.sf.cglib.proxy.Callback
at public net.sf.cglib.proxy.Callback[] foo.bar.PowermockTest$X$$EnhancerByCGLIB$$f0189e84.getCallbacks()
at foo.bar.PowermockTest$X$$EnhancerByCGLIB$$f0189e84at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
... 45 moreHere is the POM snippet so you see what versions are used:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId><version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId><artifactId>hamcrest-core</artifactId>
<version>1.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xmlmatchers</groupId>
<artifactId>xml-matchers</artifactId>
<version>0.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule</artifactId>
<version>1.4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>1.4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-classloading-xstream</artifactId>
<version>1.4.12</version>
<scope>test</scope>
</dependency>Thanks in advance!Markus--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To view this discussion on the web visit https://groups.google.com/d/msg/powermock/-/zS9iHL-Tn8QJ.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.
--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To view this discussion on the web visit https://groups.google.com/d/msg/powermock/-/Vvf-t3a273EJ.
--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To view this discussion on the web visit https://groups.google.com/d/msg/powermock/-/ePogiSbm4-oJ.
Indeed I mock the DTOs, but the problem in the link you provided is different -- that guy actually did not mock anything but simply forgot add a constructor. I do have a constructor, but the mock provided by Powermock does not. THAT is my problem. See, my original DTO works well with JAXB. Just the mock does not.
If I remember it correctly this approach will not create a CgLib proxy but rather modify the byte-code of "PowerMockTest" directly.stub(method(PowerMockTest.class, "myMethodName")).toReturn(..);
Yes, I need to mock the class, as the purpose is to provide an instance of the mock to the class under test as a parameter. If I cannot mock the class, I have to create an instance, which is a problem as ist constructor want more (non-mockable) DTOs then and so on. The target is to cut dependencies between classes, which is no possible by simply stubbing methods.
suppress(constructor(X.class));
I am not familiar to "suppres". But how does this help me? I need an instance of X.class, how to get it if the constructor is suppressed?
From: Markus KARG [mailto:mar...@headcrashing.eu]
Sent: Donnerstag, 21. Juni 2012 20:02
To: 'powe...@googlegroups.com'
Subject: RE: [powermock] PowerMock not working with JAXB
I am not familiar to "suppres". But how does this help me? I need an instance of X.class, how to get it if the constructor is suppressed?
From: powe...@googlegroups.com [mailto:powe...@googlegroups.com] On Behalf Of Johan Haleby
Sent: Donnerstag, 21. Juni 2012 07:53
I see. But in what scenario is suppression of the constructor needed then?