Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve java.lang.AssertionError when creating OkHttpClient in mockito?

Tags:

I'm trying to make some canned network respones. I have the json response for the actual request and I have Retrofit interfaces that serialize responses. I am beyond frustrated trying to set this up. What should I be doing here? It seems my options are, 1) Use a MockWebServer() 2) Use a RequestInterceptor().

While trying to use either 1 or 2, I can't for the life of me instantiate an OkHttpClient() without it failing, basically this puts every thing I try to death immediately. I get an java.lang.AssertionError because OkHttpClient is throwing this when it can't find a TLS algorithm.

 if (builder.sslSocketFactory != null || !isTLS) {
      this.sslSocketFactory = builder.sslSocketFactory;
    } else {
      try {
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, null, null);
        this.sslSocketFactory = sslContext.getSocketFactory();
      } catch (GeneralSecurityException e) {
        **throw new AssertionError(); // The system has no TLS. Just give up.**
      }
    }

I've tried to keep the "javax.net.ssl" class in the android.jar using unMock, but that didn't resolve the error.

unMock {
    // URI to download the android-all.jar from. e.g. https://oss.sonatype.org/content/groups/public/org/robolectric/android-all/
    downloadFrom 'https://oss.sonatype.org/content/groups/public/org/robolectric/android-all/4.3_r2-robolectric-0/android-all-4.3_r2-robolectric-0.jar'

    keep "android.util.Log"
    keep "javax.net.ssl"
}

So basically, I've come across various examples of how to mock network requests with retrofit 2, but I can't get past this hurdle, and I'm feeling pretty defeated. I haven't seen anyone else with this problem, and I'm baffled as to how everyone is easily instantiating new OkHttpClients in all their tests.

Here are the relevant dependencies I am using.

    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-all:1.10.19'
    testCompile 'org.powermock:powermock-mockito-release-full:1.6.4'
    testCompile 'org.powermock:powermock-module-junit4:1.6.4'
    testCompile 'org.easymock:easymock:3.4'
    testCompile 'org.powermock:powermock-api-easymock:1.6.4'
    testCompile 'com.squareup.okhttp3:mockwebserver:3.2.0'

    compile 'com.squareup.retrofit2:retrofit:2.0.0'
    compile 'com.squareup.retrofit2:converter-gson:2.0.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
    compile 'com.google.code.gson:gson:2.4'
like image 930
Tyler Pfaff Avatar asked Mar 23 '16 00:03

Tyler Pfaff


1 Answers

You need this annotation at the top of the class. Of course.

@PowerMockIgnore("javax.net.ssl.*")
like image 92
Tyler Pfaff Avatar answered Sep 21 '22 15:09

Tyler Pfaff