2016-06-15 10 views
1

robolectric를 통해 응용 프로그램 클래스를 실행하려고 할 때 이상한 예외가 발생합니다. 나는 그것에 몇 시간 갇혀있다. 누군가 나를 도와 줄 수 있습니까? 내 코드는 다음입니다단위 테스트 중 Application 클래스에서 오류 발생

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

'extends ApplicationTestCase'를 제거하십시오. 이미 JVM에서 실행 중이며 장치에서는 실행하지 않았습니다. 옳은? –

+0

@EugenMartynov : 예 JVM에서 실행하고 있습니다. null이 될 테스트 케이스에 대한 어플리케이션 인스턴스가 필요합니다. 그래서 나는 createApplication()이 애플리케이션 인스턴스를 생성 할 것이라고 조사했다. 만약 내가 ApplicationTestCase 다음 응용 프로그램 인스턴스를 얻을 수있을 것이라고 제거 할 것입니다? –

+0

네, 고맙습니다. –

답변

1
악기 테스트를 JUnit 테스트를 혼합하지 마십시오

: 당신의 인스턴스가 필요한 경우

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

응용 프로그램은 RuntimeEnvironment.application을 사용합니다. 응용 프로그램 자체를 테스트하려면 new을 사용하여 메서드를 호출하고 메서드를 호출해야하지만 Robolectric 테스트가 아니어야합니다.

관련 문제