当我只运行方法testInsert()测试完成时没有问题,但是当我运行整个类时,我得到了一个错误:
java.lang.NoSuchMethodError: com.dataart.kvarivoda.myapplication.api.PlacesApi.getNearbyPlaces(Ljava/lang/String;Ljava/lang/String;I)Lretrofit2/Call;
at GetPlacesTest.testLogin(GetPlacesTest.java:61)
....
Retrofit @GET接口是:
public interface PlacesApi {
@GET("/maps/api/place/nearbysearch/json")
PlacesResults getNearbyPlaces(@Query("key") String key, @Query("location") String location, @Query("radius") int radius);
}
我的测试类是:
public class GetPlacesTest extends ProviderTestCase2 {
PlacesApi mockApi;
EventBus mockEventBus;
PlacesApi api;
Cursor cursor;
String webApiKey;
public GetPlacesTest() {
super(PlacesContentProvider.class, "com.example.myName.myapplication.database.PROVE");
}
public void setUp() throws Exception {
super.setUp();
setContext(InstrumentationRegistry.getTargetContext());
//MockitoAnnotations.initMocks(this);
mockApi = Mockito.mock(PlacesApi.class);
mockEventBus = Mockito.mock(EventBus.class);
webApiKey = getContext().getResources().getString(R.string.webApiKey);
api = ((PlacesApp) getApplication()).getApi();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
if (cursor != null) {
cursor.close();
}
}
@Test
public void testInsert() throws Exception {
PlacesResults results1 = loadResults("getplaces.json");
Mockito.when(mockApi.getNearbyPlaces(eq("testkey"), Matchers.anyString(), Matchers.anyInt())).thenReturn(results1);
GetPlacesAction action = new GetPlacesAction(getContext().getContentResolver(), mockEventBus, mockApi, "testkey");
action.downloadPlaces();
//check
cursor = getContext().getContentResolver().query(PlacesContentProvider.getUri(DB.PlaceTable.DB_TABLE), null, null, null, null);
assertEquals(2, cursor.getCount());
cursor.moveToPosition(0);
assertEquals("461d123aeb1c1648abdd5e535989d2bc518cf28e", getColumn(DB.PlaceTable.COLUMN_ID));
assertEquals("Astral Tower & Residences", getColumn(DB.PlaceTable.COLUMN_TITLE));
assertEquals(getImageUrl("CoQBcwAAAMUTbLLt7doNLiVSnpGryeIJLVdrDnMPcqs3uV84zfvDklrBr1uYxitVMEZWzTD40xkM923ak8HfRtoGiNdi32mqzP6sKB3lOYNbbOQeaHZ3bStClwhWO3507ryh4bODvEfXc-l42r7rFXFAg9GLSd7N2tqoOgLwzTLray0d1sixEhAZaZ2_ajvBieZvUuPA72d7GhQTtFtpqT8j7UBYSHvq9AuRsoRSig"), getColumn(DB.PlaceTable.COLUMN_IMAGE));
assertEquals(locationToString(-33.868111), getColumn(DB.PlaceTable.COLUMN_LOCATION_LAT));
assertEquals(locationToString(151.195219), getColumn(DB.PlaceTable.COLUMN_LOCATION_LNG));
}
private PlacesResults loadResults(String file) throws IOException {
InputStream is = InstrumentationRegistry.getContext().getAssets().open(file);
return new GsonBuilder().create().fromJson(new InputStreamReader(is), PlacesResults.class);
}
private String getImageUrl(String photoReference) {
return new GoogleImagesUtil("testkey").getImageUrl(photoReference);
}
private String locationToString(double location) {
return String.valueOf(LocationsUtil.locationToInteger(location));
}
private String getColumn(String column) {
return cursor.getString(cursor.getColumnIndex(column));
}
private int getColumnInt(String column) {
return cursor.getInt(cursor.getColumnIndex(column));
}
}
经过一些尝试后,我甚至无法编译并开始得到" No tests found "错误。后来我发现类上有一些运行配置,尽管我没有添加任何内容。我删除了以下答案的运行配置:https://stackoverflow.com/a/38190125/2574228
所有的错误都在那之后。