Using MockWebServer with Robolectric

I am trying to use unit test some API calls using MockWebServer and Robolectric.

My test class is annotated with:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 23)

However, when trying to create an instance of Retrofit, the following exception occurs:

java.lang.NullPointerException
    at android.os.Handler.__constructor__(Handler.java:229)
    at android.os.Handler.<init>(Handler.java)
    at retrofit2.Platform$Android$MainThreadExecutor.<init>(Platform.java:105)
    at retrofit2.Platform$Android.defaultCallbackExecutor(Platform.java:97)
    at retrofit2.Retrofit$Builder.build(Retrofit.java:556)

The code I use to create an instance of the modification:

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(mMockServer.url(""))
                .addConverterFactory(GsonConverterFactory.create())
                .build();

The exception above is returned when called .build().

How to fix this problem?

+4
source share
4 answers

. , Robolectric 3.0 , dave-r12 , , .

@RunWith(RobolectricGradleTestRunner.class)
@Config(shadows = CreateOkHttpClientTest.MyNetworkSecurityPolicy.class)
public class CreateOkHttpClientTest {

    @Test
    ...

    @Implements(NetworkSecurityPolicy.class)
    public static class MyNetworkSecurityPolicy {

        @Implementation 
        public static NetworkSecurityPolicy getInstance() {
            try {
                Class<?> shadow = MyNetworkSecurityPolicy.class.forName("android.security.NetworkSecurityPolicy");
                return (NetworkSecurityPolicy) shadow.newInstance();
            } catch (Exception e) {
                throw new AssertionError();
            }
        }

        @Implementation 
        public boolean isCleartextTrafficPermitted() {
            return true;
        }
    }

}
+3

sdk = 21. , sdk = 23 ​​ 3.1-SNAPSHOT .

: , ​​ 3.1,

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 23)
public class ExampleUnitTest {

private MockWebServer mMockServer = new MockWebServer();

    @Test
    public void build_retrofit() throws Exception {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(mMockServer.url(""))
                .addConverterFactory(GsonConverterFactory.create())
                .build();

   }
}

My gradle.build

testCompile 'junit:junit:4.12'
testCompile "org.robolectric:robolectric:3.1"
testCompile 'com.squareup.okhttp3:mockwebserver:3.3.0'

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'

?

0

, , , , - Retrofit

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://www.example.com/")
            .client(client)
            .callbackExecutor(new Executor() {
                @Override
                public void execute(Runnable command) {
                    //doesn't matter since tests are synchronous
                }
            })
            .addConverterFactory(ScalarsConverterFactory.create())
            .build();

-, "", - , Robolectric - .

0

If you are stuck on Robolectric 3 or lower and you are targeting API 25, the correct solution is almost the same as the one made.

@Implements(NetworkSecurityPolicy.class)
public class NetworkSecurityPolicyShadow {

    @Implementation
    public static NetworkSecurityPolicy getInstance() {
        try {
            Class<?> shadow = Class.forName("android.security.NetworkSecurityPolicy");
            return (NetworkSecurityPolicy) shadow.newInstance();
        } catch (Exception e) {
            throw new AssertionError();
        }
    }

    @Implementation
    public boolean isCleartextTrafficPermitted(String host) {
        return true;
    }
}

The only difference is isCleartextTrafficPermitted(String host)because OkHttp will try to call this method with an argument host.

0
source

All Articles