簡體   English   中英

Robolectric單元測試引發異常

[英]Robolectric Unit Test throws Exception

我是整個測試工作的初學者。 我想用Robolectic建立一個活動,但是如果我運行測試,它總是會拋出NullPointerException

我的測試設置

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class HomeActivityTest  {

private HomeActivity activity;


@Before
public void setup() throws Exception{
  activity   = Robolectric.buildActivity(HomeActivity.class)
            .create()
            .resume()
            .get();
}

我的StackTrace

java.lang.NullPointerException
at org.robolectric.res.ThemeStyleSet$OverlayedStyle.equals(ThemeStyleSet.java:67)
at org.robolectric.res.ThemeStyleSet.apply(ThemeStyleSet.java:29)
at org.robolectric.shadows.ShadowAssetManager.applyThemeStyle(ShadowAssetManager.java:729)
at android.content.res.AssetManager.applyThemeStyle(AssetManager.java)
at android.content.res.ResourcesImpl$ThemeImpl.applyStyle(ResourcesImpl.java:1177)
at android.content.res.Resources$Theme.applyStyle(Resources.java:1404)
at android.view.ContextThemeWrapper.onApplyThemeResource(ContextThemeWrapper.java:186)
at android.app.Activity.onApplyThemeResource(Activity.java:4248)
at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:198)
at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:140)
at android.app.Activity.setTheme(Activity.java:4240)
at org.robolectric.shadows.ShadowActivity.callAttach(ShadowActivity.java:216)
at org.robolectric.android.controller.ActivityController.attach(ActivityController.java:41)
at org.robolectric.android.controller.ActivityController.of(ActivityController.java:25)
at org.robolectric.Robolectric.buildActivity(Robolectric.java:84)
at org.robolectric.Robolectric.buildActivity(Robolectric.java:80)
at com.my.application.activities.HomeActivityTest.setup(HomeActivityTest.java:32)
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 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:253)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:130)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:42)
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.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:84)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

我已經嘗試過該解決方案Android Studio 2.3更新失敗了,但它對我不起作用。

我昨天在升級我的一個舊項目的部門時遇到了這個問題。 原來在robolectric 3.3+中添加了“與工具鏈更緊密的集成”。 對我來說,解決方法是執行robolectric的“入門”頁面( http://robolectric.org/getting-started/ )的build.gradle ,並將以下內容添加到我的應用程序的build.gradle文件中。

android {
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}

我正在使用的依賴項:

  • Gradle包裝器:v4.1
  • android gradle插件:v3.0.1
  • robolectric:v3.8
  • Android編譯SDK:26

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM