![](/img/trans.png)
[英]Java unit tests failing with NoClassDefFoundError while running using mvn test but are passing using intelliJ UI
[英]NoClassDefFoundError while running Roboelectric Unit Test
我正在嘗試運行 Roboelectric 單元測試來測試預期的活動是否開始,但由於我收到以下錯誤:
java.lang.NoClassDefFoundError: 無法初始化類 android.os.AsyncTask
我正在尋找解決方案,但到目前為止還沒有運氣,以前有沒有人遇到過這個問題。
在我的測試課下面找到
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, manifest = "AndroidManifest.xml", minSdk = 21, maxSdk = 21, application = MyApplication.class)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
public class PushManagerTest {
@Test
public void shouldLaunchNewActivity() throws Exception {
Activity testActivity = Robolectric.setupActivity(TestActivity.class);
Intent expectedIntent = new Intent(testActivity, NewActivity.class);
Intent actualIntent = ShadowApplication.getInstance().getNextStartedActivity();
assertEquals(expectedIntent.getComponent(), actualIntent.getComponent());
}
}
我終於修好了,不知道是不是正確的方法,但測試按預期工作
我必須在我的 Application 類的onCreate
添加以下幾行
try{
Class.forName("android.os.AsyncTask");
}catch(Throwable ignore) {
// ignored
}
希望它可以幫助某人
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.