[英]Android unit tests - IncompatibleClassChangeError when running MockServer with Robolectric
我有這個測試課...
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(),一切似乎都還可以。 當然這不是很好嗎?
這似乎是1.2.2 https://github.com/square/okhttp/issues/357中修復的以下問題
如果您使用的是較早版本,請更新至最新版本,然后查看是否仍會發生。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.