我想测试一个MVP模式。所以我有一个Presenter类,当点击某些按钮时,它将调用View方法。现在我要验证,Presenter真的调用了Method,所以我写了这些测试:
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public class MainPresenterTest {
@Mock
private MainContract.View view;
private MainPresenter presenter;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
presenter = new MainPresenter(view);
}
@Test
public void handleScanButtonClicked() {
presenter.handleScanButtonClicked();
verify(view).showScanScreen();
}
@Test
public void handleBackButtonClicked() {
presenter.handleBackButtonClicked();
verify(view).showMainScreen();
}
}
public class MainPresenter implements MainContract.Presenter {
private final MainContract.View view;
public MainPresenter(MainContract.View view) {
this.view = view;
}
@Override
public void handleScanButtonClicked() {
view.showScanScreen();
}
@Override
public void handleBackButtonClicked() {
view.showMainScreen();
}
}
public interface MainContract {
/** handles UI-interaction **/
interface Presenter {
void handleScanButtonClicked();
void handleBackButtonClicked();
}
/** handles showing/hiding UI-elements and screens **/
interface View {
void showScanScreen();
void showMainScreen();
void showWebViewScreen();
}
}
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-all:1.10.19'
Wanted but not invoked:
view.showMainScreen();
-> at com.whatsthat.androidapp.MainPresenterTest.handleBackButtonClicked(MainPresenterTest.java:36)
Actually, there were zero interactions with this mock.
Wanted but not invoked:
view.showMainScreen();
-> at com.whatsthat.androidapp.MainPresenterTest.handleBackButtonClicked(MainPresenterTest.java:36)
Actually, there were zero interactions with this mock.
at com.whatsthat.androidapp.MainPresenterTest.handleBackButtonClicked(MainPresenterTest.java:36)
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 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 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.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
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.AppMainV2.main(AppMainV2.java:131)
Wanted but not invoked:
view.showScanScreen();
-> at com.whatsthat.androidapp.MainPresenterTest.handleScanButtonClicked(MainPresenterTest.java:30)
Actually, there were zero interactions with this mock.
Wanted but not invoked:
view.showScanScreen();
-> at com.whatsthat.androidapp.MainPresenterTest.handleScanButtonClicked(MainPresenterTest.java:30)
Actually, there were zero interactions with this mock.
at com.whatsthat.androidapp.MainPresenterTest.handleScanButtonClicked(MainPresenterTest.java:30)
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 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 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.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
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.AppMainV2.main(AppMainV2.java:131)
Process finished with exit code -1
当我现在在没有AVD运行的情况下运行测试时,它们将失败。我想只在我的本地计算机上运行这些测试,并且应该没有特定的Android交互,对吧?但是为了让测试通过,我需要启动一个AVD,并构建+部署APK。
为什么会这样,我怎样才能在本地机器上正确运行这类测试?
提前致谢
答案 0 :(得分:0)
你没有告诉框架遇到这一行时它应该做什么
view.showScanScreen()
和
view.showMainScreen()
做
doNothing().when(view).showScanScreen();
doNothing().when(view).showMainScreen();
你的测试方法将是这样的
@Test
public void handleScanButtonClicked() {
doNothing().when(view).showScanScreen();
presenter.handleScanButtonClicked();
verify(view).showScanScreen();
}
@Test
public void handleBackButtonClicked() {
doNothing().when(view).showMainScreen();
presenter.handleBackButtonClicked();
verify(view).showMainScreen();
}
答案 1 :(得分:0)
如果要在本地计算机的JVM上运行测试,则需要将它们放在test
包中,而不是放在androidTest
包中。 androidTest
用于检测测试,需要运行Android框架。
查看此post的差异。