【问题标题】:Getting error in Application class during unit testing在单元测试期间在应用程序类中出现错误
【发布时间】:2016-06-15 09:33:42
【问题描述】:

当我尝试通过 robolectric 执行我的应用程序类时,我遇到了奇怪的异常。 我被困了几个小时。 有人可以帮我吗? 我的代码 sn-p 如下:

 java.lang.RuntimeException: Stub!

at android.test.ApplicationTestCase.__constructor__(ApplicationTestCase.java:5)
at android.test.ApplicationTestCase.<init>(ApplicationTestCase.java)
at com.grapplemobile.tmcplus.TmcPlusApplicationTest.<init>(TmcPlusApplicationTest.java:26)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.createTest(RobolectricTestRunner.java:520)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.methodBlock(RobolectricTestRunner.java:530)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:247)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
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.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
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:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

我的代码如下:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class,sdk = 18)
public class MyApplicationTest extends ApplicationTestCase<MyApplication>{
  private MyApplication application;

   public  MyApplicationTest ()
  {
      super(MyApplication.class);
   }
   @Before
    public void setUp() throws Exception {
      // super.setUp();
      createApplication();
      application = getApplication();

   }

   @Test
   public void testonCreate() throws Exception {
   application.onCreate();
  }

【问题讨论】:

  • 请删除extends ApplicationTestCase。您已经从 JVM 而不是在设备上运行它。对吗?
  • @EugenMartynov:是的,我从 JVM 运行它。我的测试用例需要应用程序实例,它即将到来 null 。所以我搜索了 createApplication() 将创建应用程序实例。如果我将删除 ApplicationTestCase 那么我将如何获得应用程序实例?
  • 是的,你写的非常感谢!

标签: java android unit-testing junit4 robolectric


【解决方案1】:

不要将 JUnit 测试与工具测试混用:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class,sdk = 18)
public class MyApplicationTest{
}

如果您需要应用程序的实例,请使用RuntimeEnvironment.application。如果您想测试应用程序本身,只需使用new 创建它并在其上调用方法,但它不应该是Robolectric test

【讨论】:

  • RuntimeEnvironment.application 已弃用
猜你喜欢
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 2020-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多