我正在制作应用,其主要活动有一个按钮。按下它时,它会启动AsyncTask。此任务从云中获取字符串(现在它从我的google端点开发服务器获取),然后在“onPostExecute”方法上启动显式意图以打开另一个活动。
我编写了以下测试来验证点击按钮实际上是否产生了意图:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class ApplicationTest {
@Rule
public IntentsTestRule<MainActivity> mActivityRule =
new IntentsTestRule<MainActivity>(MainActivity.class);
@Test
public void AsyncTaskFetchesJoke() {
onView(withId(R.id.instructions_text_view))
.perform(click());
intended(allOf(
toPackage("com.example.madelenko.jokeactivity"),
hasExtras(hasEntry(equalTo(MainActivity.JOKE), equalTo(JokeDispenser.getJoke())))
));
}
}
另外,这是我的asyncTask:
public class FetchJokeTask extends AsyncTask<Context, Void, String> {
private static MyApi myApiService = null;
private Context context;
@Override
protected String doInBackground(Context ... params) {
if(myApiService == null) { // Only do this once
MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
.setRootUrl("http://10.0.2.2:8080/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
// end options for devappserver
myApiService = builder.build();
}
context = params[0];
try {
MyBean bean = myApiService.supplyJoke().execute();
return bean.getData();
} catch (IOException e) {
return e.getMessage();
}
}
@Override
protected void onPostExecute(String result) {
Intent intent = new Intent(context, JokeActivity.class);
intent.putExtra(MainActivity.JOKE, result);
context.startActivity(intent);
}
}
出于某种原因,我的测试失败并吐出以下追溯:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 0 intents.
IntentMatcher: (resolvesTo: com.example.madelenko.jokeactivity and has extras: has bundle with: key: "joke" value: "!false: it's funny because it's true.")
Matched intents:[]
Recorded intents:[]
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:92)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158)
at android.support.test.espresso.intent.Intents.intended(Intents.java:187)
at android.support.test.espresso.intent.Intents.intended(Intents.java:169)
at com.udacity.gradle.builditbigger.ApplicationTest.AsyncTaskFetchesJoke(ApplicationTest.java:37)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
Caused by: junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents.
你能帮我理解为什么吗?
非常感谢。