开发者社区> 问答> 正文

Junit getContext 为null ,环境已配置 配置报错 

就是我想搭建测试环境,但是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🏃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)

展开
收起
kun坤 2020-06-03 14:40:50 482 0
1 条回答
写回答
取消 提交回答
  • 空指针,看看你的setUp方法 getApplication()是不是为空。######

    引用来自“landcoder”的评论

    空指针,看看你的setUp方法 getApplication()是不是为空。 就是它为空啊,getContext,getService,getSystemContext都为空,问题是我不知道怎么解决,它是junit测试用例的,应该是环境配置问题的吧? 但是我照着网上那样配置了,还是不行呢?不知道漏了什么。
    2020-06-04 10:35:19
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载