简体   繁体   English

Android单元测试-使用Robolectric运行MockServer时出现IncompatibleClassChangeError

[英]Android unit tests - IncompatibleClassChangeError when running MockServer with Robolectric

I have this test class... 我有这个测试课...

package com.blah.blah;

import static org.junit.Assert.assertTrue;

import com.squareup.okhttp.mockwebserver.MockWebServer;

import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class IncompatibleClassChangeErrorTest {

    private MockWebServer server;

    @Before
    public void setup() throws IOException {
        server = new MockWebServer();
        server.play();
    }

    @After
    public void teardown() throws IOException {
        server.shutdown(); // TODO Fix if possible. This throws java.lang.IncompatibleClassChangeError
    }

    @Test
    public void test() {
        assertTrue(true);
    }
}

...and the following exception stack shows up in the consoles of both Eclipse and Android Studio... ...并且以下异常堆栈显示在Eclipse和Android Studio的控制台中...

Exception in thread "pool-2-thread-1" java.lang.IncompatibleClassChangeError: Class java.net.ServerSocket does not implement the requested interface java.io.Closeable
    at com.squareup.okhttp.internal.Util.closeQuietly(Util.java:110)
    at com.squareup.okhttp.mockwebserver.MockWebServer$2.run(MockWebServer.java:249)
    at com.squareup.okhttp.mockwebserver.MockWebServer$4.run(MockWebServer.java:624)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:695)

...otherwise the test passes. ...否则测试通过。 Also running the test from the command line with Gradle just gives a Success message. 同样,使用Gradle从命令行运行测试只会给出一条成功消息。

If I comment out the server.shutdown(), everything seems OK. 如果我注释掉server.shutdown(),一切似乎都还可以。 Surely this can't be good though? 当然这不是很好吗?

This seems to be the following issue that was fixed in 1.2.2 https://github.com/square/okhttp/issues/357 这似乎是1.2.2 https://github.com/square/okhttp/issues/357中修复的以下问题

If you're using an earlier version, update to latest and see if it still happens. 如果您使用的是较早版本,请更新至最新版本,然后查看是否仍会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM