4

任何人都知道错误是什么意思?或者也许错过了什么?

试图从这个样本开始,

添加了依赖项:testImplementation 'com.squareup.okhttp3:mockwebserver:3.11.0'

代码只是findMessage_receivesAScriptedResponseFromMockBackend在 AndroidStudio 的示例单元测试中添加了一个 @Test:

    public class ExampleUnitTest {
        @Test
        public void addition_isCorrect() {
            assertEquals(4, 2 + 2);
        }

    ///
    @Rule
    public MockWebServer mockBackend = new MockWebServer();

    private MessagesApi mockApi() {
        return new MessagesApi.Builder()
                .baseUrl(mockBackend.url("/").toString())
                .build();
    }

    @Test
    public void builder_returnsInstance() {
        final MessagesApi messagesApi = new MessagesApi.Builder()
                .baseUrl("http://localhost:8888")
                .build();

        assertNotNull(messagesApi);
    }

@Test
public void findMessage_receivesAScriptedResponseFromMockBackend() throws IOException {
    mockBackend.enqueue(
            new MockResponse()
                    .setBody("{\"text\":\"hello!\"}")
                    .setResponseCode(200)
                    .addHeader("Content-Type", "application/json;charset=utf-8")
    );

    final Response<Message> response = mockApi().findMessage("123").execute();

    assertTrue((response.code()) == (200));
    assertTrue((response.headers().get("Content-Type")).contains("application/json"));

    final ResponseBody rawResponseBody = response.raw().body();
    assertTrue((rawResponseBody.contentType().type()).equals("application"));
    assertTrue((rawResponseBody.contentType().subtype()).equals("json"));
}

public class Message {

    public String text;
}

public interface MessagesApi {

    @GET("/message")
    Call<Message> findMessage(@Query("query") String keyword);


    /** Convenient builder for MessagesApi */
    class Builder {
        private String baseUrl;

        public Builder() {}

        public Builder baseUrl(String baseUrl) {
            this.baseUrl = baseUrl;

            return this;
        }

        public MessagesApi build() {
            return new Retrofit.Builder()
                    .client(new OkHttpClient.Builder().build())
                    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(baseUrl)
                    .build()
                    .create(MessagesApi.class);
        }
    }
}
}

运行测试时显示错误:

Oct 17, 2018 5:58:57 PM okhttp3.mockwebserver.MockWebServer$2 execute
INFO: MockWebServer[60271] starting to accept connections
Oct 17, 2018 5:58:57 PM okhttp3.mockwebserver.MockWebServer$2 acceptConnections
INFO: MockWebServer[60271] done accepting connections: Socket closed
Oct 17, 2018 5:58:57 PM okhttp3.mockwebserver.MockWebServer$2 execute
INFO: MockWebServer[60272] starting to accept connections
Exception in thread "MockWebServer" java.lang.NoSuchFieldError: H2_PRIOR_KNOWLEDGE
at okhttp3.mockwebserver.MockWebServer$3.processConnection(MockWebServer.java:487)
at okhttp3.mockwebserver.MockWebServer$3.execute(MockWebServer.java:442)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Oct 17, 2018 5:59:07 PM okhttp3.mockwebserver.MockWebServer$2 acceptConnections
INFO: MockWebServer[60272] done accepting connections: Socket closed

java.net.SocketTimeoutException: timeout
4

2 回答 2

2

似乎版本 3.11.0 引入了这个问题

https://github.com/spring-projects/spring-security/issues/5570

于 2018-10-18T16:37:46.390 回答
1

如果有人遇到相同的问题,MockWebServer则来自相同的依赖项,OKHttp因此他们都需要具有相同的依赖项版本。

所以这个消息可能是因为 MockWebServer 找不到 OkHttp 库中定义的属性。在我的情况下,后者的版本较低,使他们的版本相等解决了这个问题。

于 2020-10-20T01:39:41.483 回答