Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PowerMock to mock static class in a rest-assured test

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.

like image 743
John Fadria Avatar asked Oct 16 '14 10:10

John Fadria


People also ask

Can static classes be mocked?

The powerful capabilities of the feature-rich JustMock framework allow you to mock static classes and calls to static members like methods and properties, set expectations and verify results. This feature is a part of the fastest, most flexible and complete mocking tool for crafting unit tests.

Can we mock static methods in Mockito?

Mockito allows us to create mock objects. Since static method belongs to the class, there is no way in Mockito to mock static methods. However, we can use PowerMock along with Mockito framework to mock static methods.

Is PowerMock a mocking framework?

PowerMock is an open-source Java framework used for creating a mock object in unit testing. It extends other mocking frameworks such as EasyMock and Mockito to enhance the capabilities.


2 Answers

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.*")

like image 160
Oscar Scholten Avatar answered Sep 30 '22 15:09

Oscar Scholten


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.*"})
like image 25
Jason D Avatar answered Sep 30 '22 14:09

Jason D