Using PowerMock to mock static class in a rest-ass

2019-04-30 22:40发布

问题:

Is there any way to use PowerMock with rest-assured, because when I'm trying to test a RESTful API with rest-assured.

I want to PowerMock a static call.

The code of operation:

@POST
@Produces("application/json")
@Consumes(MediaType.APPLICATION_JSON)
public Response createEntity(@Context HttpHeaders hh, String body) {
    . . . 

    String sec = MDI.check(Some_string, ..., ...);
    if (sec == null) throw ...

    return Response....
}

And the test:

@RunWith(PowerMockRunner.class)
@PrepareForTest(MDI.class)
public class createSOTest {

    @Test
    public void testStatic() {
        mockStatic(MDI.class);

        expect(MDI.check(Some_string, ..., ...).andReturn(Some_String);
        replay(MDI.class)

        given().
            contentType(ContentType.JSON).
            header("SomeHeader", "something").
            body(root).
        when().
            post("/").
        then().
            statusCode(...);
    }
}

The problem is that I obtain an exception when the test try to run the rest-assured code (given()....):

org.apache.http.conn.ssl.SSLInitializationException: class configured for SSLContext: sun.security.ssl.SSLContextImpl$TLS10Context not a SSLContext
    at org.apache.http.conn.ssl.SSLContexts.createDefault(SSLContexts.java:58)
    at org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory(SSLSocketFactory.java:162)
    at org.apache.http.impl.conn.SchemeRegistryFactory.createDefault(SchemeRegistryFactory.java:52)
    at org.apache.http.impl.client.AbstractHttpClient.createClientConnectionManager(AbstractHttpClient.java:305)
    at org.apache.http.impl.client.AbstractHttpClient.getConnectionManager(AbstractHttpClient.java:465)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
    at org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:73)
    at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:61)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:227)
    at com.jayway.restassured.internal.RequestSpecificationImpl.applyProxySettings(RequestSpecificationImpl.groovy:1794)

It seems to be a problem with the PowerMockRunner.class.

回答1:

I ran into the same issue and found the solution on this blog:

This is related to the SSLCOntext loading stuff from the upstream classloader - which is Power Mock's one when running the test.

Solution : use @PowerMockIgnore annotation on the top of your test class :

@PowerMockIgnore("javax.net.ssl.*")



回答2:

Oscar's answer was very helpful. I had to exclude multiple namespaces to get it to work. I ended up getting it to pass with this:

@PowerMockIgnore({"javax.management.*", "org.apache.http.conn.ssl.*", "com.amazonaws.http.conn.ssl.*", "javax.net.ssl.*"})


回答3:

This doesn't look like a powermock issue. Narrow it down by removing the static dependency within the code under test temporarily - stub it out so it returns just anything. Try the test again without any mocking. You may get the same error, in which case you can investigate that error.