【问题标题】:Android unit tests - IncompatibleClassChangeError when running MockServer with RobolectricAndroid 单元测试 - 使用 Robolectric 运行 MockServer 时出现 IncompatibleClassChangeError
【发布时间】:2014-09-18 23:39:58
【问题描述】:

我有这个测试类...

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);
    }
}

...以下异常堆栈显示在 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)

...否则测试通过。同样使用 Gradle 从命令行运行测试只会给出成功消息。

如果我注释掉 server.shutdown(),一切似乎都正常。不过这肯定不好?

【问题讨论】:

    标签: android robolectric mockwebserver


    【解决方案1】:

    这似乎是在 1.2.2 中修复的以下问题 https://github.com/square/okhttp/issues/357

    如果您使用的是早期版本,请更新到最新版本,看看是否仍然存在。

    【讨论】:

    • 确实如此。让它与 1.6.0 版和相应的 okhttp(不是 okhttp-protocols)库一起工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2017-02-03
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多