我创建了一个用Espresso
进行测试的简单案例。我有一个使用MVP的项目,当用户点击按钮时,我会在按钮下面的TextView
中显示一些文字。
我所做的是在DataSource
中注入一个Presenter
课程,然后将数据传递给View
。
以下是我在Presenter
:
public MainPresenter(@NonNull MainMVP.View mainView) {
this.mainView = mainView;
DataComponent dataComponent = DaggerDataComponent.builder()
.dataModule(new DataModule())
.build();
dataComponent.inject(this);
}
@Override
public void onButtonClick() {
if (dataSource != null) {
mainView.showData(dataSource.getReleaseString());
}
}
现在,当我测试Activity
时,我想测试when a click happens on the button the text is displayed
。
我写的测试是:
@Test
public void onButtonGetDataClicked_textViewDisplaysData() {
Mockito.when(dataSource.getReleaseString()).thenReturn(MOCK_STRING);
activityRule.launchActivity(new Intent());
/** Click Button*/
onView(withId(R.id.btn_get_data))
.perform(click());
onView(withId(R.id.tv_text_data))
.check(matches(withText(MOCK_STRING)));
}
但是a)当我运行它时,我在设备上看到“释放字符串”(来自注入的DataSource
)而不是我用于测试的“模拟字符串”,我收到以下错误:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "Mock String"' doesn't match the selected view.
Expected: with text: is "Mock String"
Got: "AppCompatTextView{id=2131427413, res-name=tv_text_data, visibility=VISIBLE, width=183, height=38, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=32.0, y=128.0, text=Release String, input-type=0, ime-target=false, has-links=false}"
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:579)
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 com.dagger.testing.testingwithdagger.main.MainActivityTest.onButtonGetDataClicked_textViewDisplaysData(MainActivityTest.java:70)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
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 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
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:1792)
Caused by: junit.framework.AssertionFailedError: 'with text: is "Mock String"' doesn't match the selected view.
Expected: with text: is "Mock String"
Got: "AppCompatTextView{id=2131427413, res-name=tv_text_data, visibility=VISIBLE, width=183, height=38, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=32.0, y=128.0, text=Release String, input-type=0, ime-target=false, has-links=false}"
at android.support.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:1053)
at android.support.test.espresso.assertion.ViewAssertions$2.check(ViewAssertions.java:89)
at android.support.test.espresso.ViewInteraction$2.run(ViewInteraction.java:170)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
对我来说测试原因有什么问题似乎没问题:传递给DataSource
模拟数据值,执行点击并检查文本是否显示。我应该更改什么才能对此用例进行正确的测试?
编辑:阅读下面的评论我改变了setUp()
,如:
@Before
public void setUp() {
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
mainView = mock(MainMVP.View.class);
mainPresenter = new MainPresenter(mainView);
DaggerDataComponent component = (DaggerDataComponent) DaggerDataComponent.builder()
.dataModule(new DataModule() {
@Override
public DataSource providesDataSource() {
return mock(DataSource.class);
}
})
.build();
component.inject(mainPresenter);
}
但现在,当我运行此功能时,我在NPE
处获得了dataSource
行:Mockito.when(dataSource.getReleaseString()).thenReturn(MOCK_STRING);