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();
}
}