就是我想搭建测试环境,但是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)
空指针,看看你的setUp方法 getApplication()是不是为空。######
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。