java - Mockito 验证模拟调用中对模拟方法的调用

标签 java unit-testing mockito

我有模型模拟类,我可以在其中验证来自另一个 DAO 模拟对象的调用。但这个调用不会发生在模型类上,只有验证发生在 DAO 类上。

public class ApplicationTests {

@Mock
private Card card; 

@Mock
private JourneyDAO journeyDAO;

@InjectMocks
private JourneyServiceImpl journeyServiceImpl;

private static final Double ZERO_VALUE = 0.0;
private static final Double INITIAL_BALANCE = 30D;
private static final String NAME = "name";

@Before
public void setUp() {
    when(card.getBalance()).thenReturn(INITIAL_BALANCE);
    when(card.getName()).thenReturn(NAME);
}


@Test
public void startJourneyInZone1_Test() {
    barrier = new Barrier(card, direction.IN, journeyType.TUBE, Stations.Holborn);
    journeyServiceImpl.startTubeJourney(barrier);
    verify(card, times(1)).addBalance(MAX_FARE * -1);// this call is not heppening
    verify(journeyDAO, times(1)).startTubeJourney(barrier);// this is working 
}

}

DAO 类方法如下所示

public void startTubeJourney(Barrier barrier) {
    if(barrier.getModeOfJourney() == TravelMode.TUBE) {
        if(barrier.getDirection() == Direction.IN) {
            boardingAtZone = barrier.getZone();
            barrier.getCard().addBalance(MAX_FARE * -1);
        }
    }
}

我在运行测试时遇到以下错误。请忽略包名和类名不匹配的情况,因为我正在制作一些示例案例。

Wanted but not invoked:
card.addBalance(0.7000000000000002d);
-> at com.rcard.travel.ApplicationTests.endTubeJourneyTest(ApplicationTests.java:69)

Actually, there were zero interactions with this mock.

at com.travel.ApplicationTests.endTubeJourneyTest(ApplicationTests.java:69)
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.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

屏障类看起来像

public class Barrier {

    private Card card;
    private Direction direction;
    private TravelMode modeOfJourney;
    private Stations zone;

    public Barrier(Card card, Direction direction, TravelMode mode, Stations zone) {
        this.card = card;
        this.direction = direction;
        this.modeOfJourney = mode;
        this.zone = zone;
    }

    public String getZone() {
        return Stations.elementOf(this.zone);
    }

    public void setZone(Stations zone) {
        this.zone = zone;
    }

    public Card getCard() {
        return card;
    }

    public void setCard(Card card) {
        this.card = card;
    }

    public Direction getDirection() {
        return direction;
    }

    public void setDirection(Direction direction) {
        this.direction = direction;
    }

    public TravelMode getModeOfJourney() {
        return modeOfJourney;
    }

    public void setModeOfJourney(TravelMode modeOfJourney) {
        this.modeOfJourney = modeOfJourney;
    }  

}

最佳答案

因为 journeyDAO 是一个模拟,这意味着真正的 journeyDAO.startTubeJourney() 没有被调用。

一种可能的解决方法是使用真正的 JourneyDAO,因此 card.addBalance() 确实会被调用,但这确实意味着一个测试正在测试这两个服务和 DAO 类。

或者,我很想为 JourneyDAO 编写一个单独的测试。这可以调用它的startTubeJourney(),并验证card.addBalance()。这意味着针对模拟 journeyDAO.startTubeJourney() 的当前 verify() 可以保留。

关于java - Mockito 验证模拟调用中对模拟方法的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49208605/

相关文章:

java - 填充需要 1000 个其他文件中的值的文件 - Java

c# - Rhino mock stub 与期望,总是选择第一个,为什么?

ruby - 单元测试中应该父类(super class)不匹配

android - 通过 Dagger Provide 模拟 AccountManager 时出现 Java.lang.reflect.InvocationTargetException。怎么修?

java - mockito spy 可以返回 stub 值吗?

java - sshj 0.9.0 抛出 NoClassDefFoundError : net/schmizz/sshj/SSHClient

java - 检查链表是否为回文(递归)

java - 在 if 语句中使用按位 &

c# - 如何确定哪个是 SUT,哪个是单元测试的合作者?

java - 意外的传递依赖版本