question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PowerMock and MockWebServer do not play well together

See original GitHub issue

This is with mockwebserver 3.4.1, powermock 1.5.1 and Kotlin 1.1.0. This is the test:

@RunWith(PowerMockRunner::class)
@PrepareForTest(String::class)
class MyTest {
    companion object {
        private val server = MockWebServer().apply { start() }

        @AfterClass
        fun tearDown(){
            server.shutdown()
        }
    }

    @Test
    fun someTest(){
    }
}

Which results in:

Exception in thread "main" java.lang.AssertionError
	at okhttp3.OkHttpClient.systemDefaultTrustManager(OkHttpClient.java:260)
	at okhttp3.OkHttpClient.<init>(OkHttpClient.java:228)
	at okhttp3.OkHttpClient.<init>(OkHttpClient.java:203)
	at okhttp3.internal.Internal.initializeInstanceForTests(Internal.java:41)
	at okhttp3.mockwebserver.MockWebServer.<clinit>(MockWebServer.java:104)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at javassist.runtime.Desc.getClassObject(Desc.java:43)
	at javassist.runtime.Desc.getClassType(Desc.java:152)
	at javassist.runtime.Desc.getType(Desc.java:122)
	at javassist.runtime.Desc.getType(Desc.java:78)
	at com.mycompany.MyTest.<clinit>(MyTest.kt:30)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.createTestInstance(PowerMockJUnit44RunnerDelegateImpl.java:186)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.createTest(PowerMockJUnit44RunnerDelegateImpl.java:171)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:193)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
	at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101)
	at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
	at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
mrifnicommented, Aug 20, 2018

try adding this @PowerMockIgnore("javax.net.ssl.*")

2reactions
yschimkecommented, Aug 27, 2017

Stab in the dark - try this https://stackoverflow.com/a/36167978/1542667

And raise a bug with the powermock library if it persists

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing with OkHttp3 and PowerMock | by Chuck Greb - Medium
OkHttp3 and PowerMock are both useful tools on Android but using them together can get you into trouble when it comes to unit...
Read more >
How to clean up mocks in spring tests when using Mockito
I think reseting the mocks should better be done after the test method, as it implies there is ... With this trick you...
Read more >
Better Integration Testing with MockWebServer - Industrial Logic
The intention was to write an integration test that did not touch the inside of the code, but only the edges. It was...
Read more >
Nifi Notice - Cloudera Documentation
Accordingly, this listing should not be construed as an admission that the listed ... Apache License 2.0 MockWebServer 4.9.1 : Apache License 2.0...
Read more >
Use MockWebServer to Mock and Test HTTP Calls - Morioh
So, how can we ensure our code that uses API services won't break on production by testing it? This is where MockWebServer comes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found