2
回答
Junit getContext 为null ,环境已配置,是不是漏了点什么?
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

就是我想搭建测试环境,但是getApplication为空,gradle环境配置也已经配置了,应该没有漏吧?找了很多网上所说的配置,但是还是失败,如果不用getApplication(),它是可以通过测试的。

getContext,getSystemContext,getService都测试过了,都不行,好像是android配置还没有配好,但是gradle那里都已经配了啊。

public class CallServiceTest extends ServiceTestCase<CallService> {

    public CallServiceTest() {
        super(CallService.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        getApplication().onLowMemory();
    }

    public void testSomething() throws Exception {
        System.out.println("1");
        assertEquals(1, 1);
        System.out.println("2");
    }
}



gradle:


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    androidTestCompile 'com.android.support:support-annotations:23.1.0'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.1'
    compile 'com.android.support:appcompat-v7:23.1.0'
    testCompile 'junit:junit:4.12'
...
}

 defaultConfig {
        applicationId "com.revenco.av_talk"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        minSdkVersion 17
        targetSdkVersion 18
        versionCode 1
        versionName "1.0"
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }
    //ADD THESE LINES:
    packagingOptions {
        exclude 'LICENSE.txt'
    }



报的错:
java.lang.NullPointerException
at com.revenco.test.CallServiceTest.setUp(CallServiceTest.java:24)
at junit.framework.TestCase.runBare(TestCase.java:139)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)


举报
mimixi666
发帖于1年前 2回/116阅
顶部