problem with @PowerMockIgnore for JAXB

1,131 views
Skip to first unread message

srikanth reddy

unread,
Feb 2, 2010, 8:51:46 AM2/2/10
to powe...@googlegroups.com
Hi
I have a horrid time using powermock (to stub static methods)
Basically  i am trying to use @PowerMockIgnore and @PrepareForTest (different class) together.

I have two different classes which have static methods. One class (say A ) uses a static block where  the following piece of code exists
static {
        try {
            jaxbContext = JAXBContext
                    .newInstance("com.pro.xml.ws.impl.bindings:com.pro.xml.ws.v14.impl.bindings:com.pro.xml.ws.secext.impl.bindings");
        } catch (JAXBException jbe) {
            throw new RuntimeException(jbe.getMessage(), jbe);
        }

This class A has some other static methods as well. I want to use these real methods so i didn't use @SuppressStaticInitializationFor ( i want to load the schema)

Other class B has some static methods which i want to stub. The problem is i am not able to use both classes at the same time.

If i  just use
@PowerMockIgnore("Fully qualified name of class A")
@RunWith(PowerMockRunner.class)

 then
i am able to load A and use the real methods on A. The problem comes when i want to mock a static method in B .

if i use
PowerMockito.mockStatic(B.class);
        when(B.mymethod(anyString()))
        .thenReturn(someval);

it complains that class B is not found.


 If i add
@PrepareForTest(B.class) (tried adding this at class level and test level.  i read @PrepareForTest takes precedence over @PowerMockIgnore ). i get the following exception


java.lang.ExceptionInInitializerError
    at sun.reflect.GeneratedSerializationConstructorAccessor7.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:40)
    at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:59)
    at org.mockito.internal.creation.jmock.ClassImposterizer.createProxy(ClassImposterizer.java:111)
    at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:51)
    at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:79)
    at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:53)
    at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:80)

basically generated from JAXBContext.newInstance() as shown below

- with linked exception:
[com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Two classes have the same XML type name "ReferenceType". Use @XmlType.name and @XmlType.namespace to assign different names to them.
    this problem is related to the following location:
        at com.pro.xml.ws..secext.impl.bindings.ReferenceType
        at public javax.xml.bind.JAXBElement com.pro.xml.ws..secext.impl.bindings.ObjectFactory.createReference(com.pro.xml.ws..secext.impl.bindings.ReferenceType)
        at com.pro.xml.ws..secext.impl.bindings.ObjectFactory
        at protected java.util.List com.pro.xml.ws..secext.impl.bindings.SecurityTokenReferenceType.any
        at com.pro.xml.ws..secext.impl.bindings.SecurityTokenReferenceType
        at protected com.pro.xml.ws..secext.impl.bindings.SecurityTokenReferenceType com.pro.xml.ws..impl.bindings.RequestedReferenceType.securityTokenReference
        at com.pro.xml.ws..impl.bindings.RequestedReferenceType
        at public com.pro.xml.ws..impl.bindings.RequestedReferenceType com.pro.xml.ws..impl.bindings.ObjectFactory.createRequestedReferenceType()
        at com.pro.xml.ws..impl.bindings.ObjectFactory
    this problem is related to the following location:
        at com.pro.xml.ws.xmldsig.impl.bindings.ReferenceType
        at public javax.xml.bind.JAXBElement com.pro.xml.ws.xmldsig.impl.bindings.ObjectFactory.createReference(com.pro.xml.ws.xmldsig.impl.bindings.ReferenceType)
        at com.pro.xml.ws.xmldsig.impl.bindings.ObjectFactory
]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:163)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:286)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:372)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:337)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:244)
    at com.pro.ws.A.<clinit>(A.java:44)




If i remove @PowerMockIgnore and add @PrepareForTest(B.class) @SuppressStaticInitializationFor ("Fully qualified name of class A")
then i am able to stub static methods in B but cant use A. I want to use both of them at the same time and i believe it should be possible.

I tried adding org.objenesis, java.xml.* to ignore annotation but still no luck.
 I am definitely missing something.
Can some body point me to any docs or right usage of powermockignore annotation.

I am using latest versions powermock 1.35, mockito 1.8.2, junit 4.7

Thanks
Srikanth

Johan Haleby

unread,
Feb 2, 2010, 2:17:33 PM2/2/10
to powe...@googlegroups.com
Hi,

This looks difficult indeed, unfortunately some XML frameworks don't work well together with PowerMock. First off all there are some packages that you cannot ignore in PowerMock, among those are the objenesis packages and java.xml packages. If you want to mock classes inside the java package you need to use the "mock system class approach". How ever this is probably not the problem you're facing. I believe that the problem may be that you're preparing B for test (which you should) but JAXB tries to use B from a different classloader than the PowerMock classloader (the system classloader I assume). But it's really hard to say.. I think I would need an example to make sure. If you could create an example and attach it to the google group (not as mail) it would be great.

/Johan

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
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.

srikanth reddy

unread,
Feb 5, 2010, 9:02:50 AM2/5/10
to powe...@googlegroups.com
Hi
I can confrim that Class B is not being used by JAXB with in A (I just wrote a different class with dummy method)
I have also removed the static block and used that jaxb code in a static method in A. But still i am getting the same problem
I cant use both @PowerMockIgnore and @PrepareForTest

You may see the sample as posted here
http://pastebin.com/m63ecddb8

Thanks
Srikanth

Johan Haleby

unread,
Feb 8, 2010, 3:02:43 AM2/8/10
to powe...@googlegroups.com
I get java.lang.RuntimeException: "com.pro.xml.ws.trust.impl.bindings" doesnt contain ObjectFactory.class or jaxb.index, so I cannot reproduce.

/Johan

srikanth reddy

unread,
Feb 15, 2010, 5:49:06 AM2/15/10
to powe...@googlegroups.com
Hi
To reproduce this problem (for ObjectFactory.class)
you may  generate the sample bindings  using a sample xsd .
you can find the sample listings.xsd here
http://pastebin.com/m32ac1dbc
I generated the bindings using XJC compiler available in jdk1.6
http://java.sun.com/javase/6/docs/technotes/tools/share/xjc.html

Basically My use case is that i cannot give individual classes in context path (as there are too many)
JAXBContext.newInstance(contextPath)

rather we just give  the pkg name  where hundreds of classes are located. But i am not able to use both
@PowerMockIgnore and @PrepareForTest at the same time.

I tried using the other method JAXBContext.newInstance(contextPath,classLoader) where classLoader is Thread.currentThread().getContextClassLoader() but still unable to fix this.

Regards
Srikanth

srikanth reddy

unread,
Feb 22, 2010, 1:48:41 AM2/22/10
to powe...@googlegroups.com
Any updates on this?

Thanks
Srikanth

Johan Haleby

unread,
Feb 22, 2010, 3:06:34 AM2/22/10
to powe...@googlegroups.com
Sorry I haven't had time. If you could generate the missing files for me and attach them to the group that would help.

/Johan
Reply all
Reply to author
Forward
0 new messages