作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
java.lang.NoSuchMethodError: okhttp3.internal.Internal.initializeInstanceForTests() When creating a Mock Server on Android
我试图更新依赖,但它没有工作。
我是 Android 单元测试的新手,我想测试 API 调用是否成功。我正在开发 https://github.com/commons-app/apps-android-commons 上的开源项目(排行榜分支)
java.lang.NoSuchMethodError: okhttp3.internal.Internal.initializeInstanceForTests()V
at okhttp3.mockwebserver.MockWebServer.<clinit>(MockWebServer.java:103)
at fr.free.nrw.commons.leaderboard.LeaderboardApiTest.initTest(LeaderboardApiTest.java:35)
public class LeaderboardApiTest {
MockWebServer server;
@Before
public void initTest() {
server = new MockWebServer();
}
@Before
public void setUp() throws IOException {
server.enqueue(new MockResponse().setBody("{\n"
+ " \"status\": 200,\n"
+ " \"username\": \"user\",\n"
+ " \"category_count\": 0,\n"
+ " \"limit\": null,\n"
+ " \"avatar\": \"avatar\",\n"
+ " \"offset\": null,\n"
+ " \"duration\": \"all_time\",\n"
+ " \"leaderboard_list\": [\n"
+ " {\n"
+ " \"username\": \"user\",\n"
+ " \"category_count\": 0,\n"
+ " \"avatar\": \"avatar\",\n"
+ " \"rank\": 1\n"
+ " }\n"
+ " ],\n"
+ " \"category\": \"used\",\n"
+ " \"rank\": 1\n"
+ "}"));
server.start();
}
@Test
public void apiTest() throws IOException, JSONException {
HttpUrl httpUrl = HttpUrl.parse(NetworkingModule.TEST_TOOLS_FORGE_URL);
LeaderboardResponse response = sendRequest(new OkHttpClient(), httpUrl);
Assert.assertEquals("avatar", response.getAvatar());
Assert.assertEquals("username", response.getUsername());
Assert.assertEquals(Integer.valueOf(0), response.getRank());
Assert.assertEquals(Integer.valueOf(0), response.getCategoryCount());
}
private LeaderboardResponse sendRequest(OkHttpClient okHttpClient, HttpUrl httpUrl)
throws IOException, JSONException {
Request request = new Builder().url(httpUrl).build();
Response response = okHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
Gson gson = new Gson();
return gson.fromJson(response.body().toString(), LeaderboardResponse.class);
}
return null;
}
@After
public void shutdown() throws IOException {
server.shutdown();
}
}
最佳答案
更新 Okhttp 版本解决了问题
configurations.all {
exclude module: 'okhttp-ws'
}
implementation 'com.squareup.okhttp3:okhttp:4.8.0'
implementation 'com.squareup.okhttp3:okhttp-ws:3.4.1'
testImplementation "com.squareup.okhttp3:mockwebserver:4.8.0"
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:4.8.0'
关于java.lang.NoSuchMethodError : okhttp3. internal.Internal.initializeInstanceForTests() 在Android上创建模拟服务器时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62865787/
java.lang.NoSuchMethodError: okhttp3.internal.Internal.initializeInstanceForTests() When creating a
我是一名优秀的程序员,十分优秀!