gpt4 book ai didi

java.lang.NoSuchMethodError : okhttp3. internal.Internal.initializeInstanceForTests() 在Android上创建模拟服务器时

转载 作者:行者123 更新时间:2023-12-05 00:19:27 30 4
gpt4 key购买 nike

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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com