我正在尝试在Android Studio中运行测试,测试设置正在抛出 java.lang.NoSuchMethodError
测试:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MusicLocalDataSourceTest {
private Context context;
private final static String TITLE_1 = "TITLE 1";
private final static String ID_1 = "1";
private final static String ARTISTIC_1 = "ARTISTIC 1";
private DataSource<Music> mMusicDataSource;
private StapeDatabase mDatabase;
@Before
public void setup() {
context = InstrumentationRegistry.getTargetContext();
mDatabase = Room
.inMemoryDatabaseBuilder(context, StapeDatabase.class)
.build();
IEMusicDao dao = mDatabase.musicDao();
MusicLocalDataSource.clearInstance(); // throws error
mMusicDataSource = MusicLocalDataSource.getInstance(new SingleExecutor(), dao);
}
@After
public void cleanUp() {
mDatabase.close();
MusicLocalDataSource.clearInstance();
}
@Test
public void should_not_have_null_instance() {
assertNotNull(mMusicDataSource);
}
}
classe que estou testando:
class being tested:
public class MusicLocalDataSource implements IEMusicDataSource<Music> {
private static volatile MusicLocalDataSource INSTANCE;
private IEMusicDao musicDao;
private AppExecutors appExecutors;
private MusicLocalDataSource(@NonNull AppExecutors appExecutors, @NonNull IEMusicDao musicDao) {
this.appExecutors = appExecutors;
this.musicDao = musicDao;
}
public static MusicLocalDataSource getInstance(@NonNull AppExecutors appExecutors, @NonNull
IEMusicDao musicDao) {
if (INSTANCE == null) {
synchronized (MusicLocalDataSource.class) {
if (INSTANCE == null) {
INSTANCE = new MusicLocalDataSource(appExecutors, musicDao);
}
}
}
return INSTANCE;
}
@Override
public void findAll(@NonNull LoadDataCallback<Music> callback) {
Runnable runnable = () -> {
final List<Music> musics = musicDao.findAll();
this.appExecutors.mainThread().execute(() -> {
if (musics.isEmpty()) {
callback.onDataNotAvailable();
} else {
callback.onDataLoaded(musics);
}
});
};
appExecutors.diskIO().execute(runnable);
}
@Override
public void findById(@NonNull String dataId, @NonNull GetDataCallback<Music> callback) {
Runnable runnable = () -> {
final Music music = musicDao.findMusicById(dataId);
appExecutors.mainThread().execute(() -> {
if(music != null) {
callback.onDataLoaded(music);
} else {
callback.onDataNotAvailable();
}
});
};
appExecutors.diskIO().execute(runnable);
}
@Override
public void save(@NonNull Music data) {
}
@Override
public void update(@NonNull Music data) {
}
@Override
public void delete(@NonNull Music data) {
}
@Override
public void delete(@NonNull String dataId) {
}
@Override
public void deleteAll() {
}
@VisibleForTesting
static void clearInstance() {
INSTANCE = null;
}
迹:
java.lang.NoSuchMethodError: No static method clearInstance()V in class
LCOM / stapeapp /镫/音乐/域/数据源/ MusicLocalDataSource;要么 它的超级类(声明 'com.stapeapp.stape.music.domain.datasource.MusicLocalDataSource' 出现在/data/app/com.stapeapp.stape.mock-2/base.apk) 在com.stapeapp.stape.music.domain.datasource.MusicLocalDataSourceTest.cleanUp(MusicLocalDataSourceTest.java:56) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50) 在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在android.support.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:80) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290) 在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71) 在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58) 在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268) 在org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在org.junit.runners.Suite.runChild(Suite.java:128) 在org.junit.runners.Suite.runChild(Suite.java:27) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290) 在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71) 在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58) 在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268) 在org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在org.junit.runner.JUnitCore.run(JUnitCore.java:137) 在org.junit.runner.JUnitCore.run(JUnitCore.java:115) 在android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58) 在android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375) 在android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1871)
有人知道发生了什么和/或可以给我一些指示吗?
修改 当我在其他测试中创建实体的新实例时,再次发生此错误,此实体是测试中的静态属性。
public class IEMusicDaoTest {
private static final Music MUSIC = new Music("1", "title", "artistic"); // error occurred here
private StapeDatabase mDatabase;
@Before
public void setUp() throws Exception {
Context context = InstrumentationRegistry.getContext();
mDatabase = Room.inMemoryDatabaseBuilder(context, StapeDatabase.class).build();
}
@After
public void tearDown() throws Exception {
mDatabase.close();
}
@Test
public void should_have_success_in_save_music() {
mDatabase.musicDao().save(MUSIC);
Music loaded = mDatabase.musicDao().findMusicById(MUSIC.getId());
assertMusic(loaded, MUSIC.getId(), MUSIC.getName(), MUSIC.getArtist());
}
private void assertMusic(Music loaded, String id, String name, String artist) {
Assert.assertThat(loaded, CoreMatchers.notNullValue());
Assert.assertThat(loaded.getId(), CoreMatchers.is(id));
Assert.assertThat(loaded.getName(), CoreMatchers.is(name));
Assert.assertThat(loaded.getArtist(), CoreMatchers.is(artist));
}
答案 0 :(得分:0)
通常检查引入该类方法的版本。如果该方法是在较新版本中引入的,编译器将接受,但如果在较旧的平台上运行,它将是运行时的错误!