我正在测试AsyncTask的结果。由于我使用的是本地Google Cloud Endpoint,因此在stopappengine,重新生成项目和sartappengine之后,测试将运行至完成。但是,当我更改断言表达式时,它在下面给了我这个奇怪的NullPointerException。
观察:当我重建项目时,测试将使用断言表达式(“ ==”或“!=”)运行到完成。不管是哪一个断言表达式,如果我更改它,它都会给我相同的e NullPointerException。然后测试再也不会正常运行,总是给我同样的错误。
JUnitTest:
@RunWith(AndroidJUnit4.class)
public class EndPointAsyncTaskTest {
@Test
public void testDoInBackground() throws Exception {
MainActivityFragment fragment = new MainActivityFragment();
EndpointsAsyncTask endpointsAsyncTask = new EndpointsAsyncTask();
fragment.testFlag = true;
endpointsAsyncTask.execute(fragment);
Thread.sleep(5000);
assertTrue("Error: Fetched Joke = " + fragment.jokeContent,
fragment.jokeContent != "");
}
}
NullPointerException错误:
Test running failed: There was 1 failure:
1) Fatal exception when running tests
java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
at java.util.regex.Matcher.reset(Matcher.java:995)
at java.util.regex.Matcher.<init>(Matcher.java:174)
at java.util.regex.Pattern.matcher(Pattern.java:1006)
at android.support.test.internal.runner.TestRequestBuilder$MethodFilter.stripParameterizedSuffix(TestRequestBuilder.java:524)
at android.support.test.internal.runner.TestRequestBuilder$MethodFilter.evaluateTest(TestRequestBuilder.java:510)
at android.support.test.internal.runner.TestRequestBuilder$ParentFilter.shouldRun(TestRequestBuilder.java:145)
at android.support.test.internal.runner.TestRequestBuilder$ClassAndMethodFilter.evaluateTest(TestRequestBuilder.java:452)
at android.support.test.internal.runner.TestRequestBuilder$ParentFilter.shouldRun(TestRequestBuilder.java:145)
at org.junit.runner.manipulation.Filter$3.shouldRun(Filter.java:112)
at org.junit.runners.ParentRunner.shouldRun(ParentRunner.java:434)
at org.junit.runners.ParentRunner.filter(ParentRunner.java:382)
at org.junit.runner.manipulation.Filter.apply(Filter.java:97)
at android.support.test.internal.runner.TestRequestBuilder$LenientFilterRequest.getRunner(TestRequestBuilder.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
FAILURES!!!
Tests run: 0, Failures: 1
Empty test suite.