2016-06-15 58 views
1

當我試圖通過robolectric執行我的應用程序類時,出現了奇怪的異常。 我被困在幾個小時。 有人可以幫我嗎? 我的代碼片段如下:在單元測試期間在應用程序類中出錯

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(); 
    } 
+1

請刪除'擴展ApplicationTestCase'。您已經從JVM運行它,而不是在設備上運行它。正確? –

+0

@EugenMartynov:是的,我從JVM運行它。我需要應用程序實例來處理我的測試用例,它將變爲空所以我搜索了createApplication()將創建應用程序實例。如果我我將刪除ApplicationTestCase,那麼我將如何能夠獲得應用程序實例? –

+0

是的,你寫了謝謝! –

回答

1

不要用儀器測試混合JUnit測試:如果你需要的實例

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

該應用程序然後使用RuntimeEnvironment.application。如果你想測試應用程序本身,然後創建它與new和調用它的方法,但它不應該Robolectric測試