gpt4 book ai didi

java.lang.NoSuchMethodError : No static method clearInstance()

转载 作者:太空宇宙 更新时间:2023-11-04 10:33:13 28 4
gpt4 key购买 nike

我尝试在 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/stape/music/domain/datasource/MusicLocalDataSource; or its super classes (declaration of 'com.stapeapp.stape.music.domain.datasource.MusicLocalDataSource' appears in /data/app/com.stapeapp.stape.mock-2/base.apk) at com.stapeapp.stape.music.domain.datasource.MusicLocalDataSourceTest.cleanUp(MusicLocalDataSourceTest.java:56) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at android.support.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:80) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375) at 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));
}

最佳答案

通常检查引入该类方法的版本。如果该方法是在新版本中引入的,编译器会接受,但如果在旧平台上运行,这将是运行时的错误!

关于java.lang.NoSuchMethodError : No static method clearInstance(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49780437/

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