gpt4 book ai didi

java - Mockito 验证仅在 AVD 运行时通过

转载 作者:行者123 更新时间:2023-11-28 20:50:42 27 4
gpt4 key购买 nike

我想测试一个 MVP 模式。所以我有一个 Presenter 类,它会在单击某些按钮时调用 View 方法。现在我想验证 Presenter 是否真的调用了方法,所以我编写了这些测试:

测试类:

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();

}
}

主合约接口(interface)

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();
}
}

我在 build.gradle 中的测试依赖:

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

我的项目结构

project structure

当我现在在没有运行 AVD 的情况下运行测试时,它们将失败。我只想在我的本地机器上运行这些测试,并且不应该专门与 Android 交互,对吧?但是为了让测试通过,我需要启动一个 AVD,然后构建 + 部署 APK。

这是为什么,我如何才能在我的本地机器上正确运行这些测试?

提前致谢

最佳答案

你没有告诉框架它遇到这一行时应该做什么

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();
}

关于java - Mockito 验证仅在 AVD 运行时通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48488161/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com