我有这个测试课...
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(),一切似乎都正常。这肯定不是好事吗?