使用 Mockito 2 模拟服务导致存根错误

编程入门 行业动态 更新时间:2024-10-20 20:40:12
本文介绍了使用 Mockito 2 模拟服务导致存根错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试使用 Mockito 模拟类的行为.这使用 Mockito 1.x 工作.迁移到 JUnit 5 和 Mockito 2 似乎不再起作用.

I try to simulate the behaviour of a class, using Mockito. This worked using Mockito 1.x. Migrating to JUnit 5 and Mockito 2 it seems not to work anymore.

@ExtendWith(MockitoExtension.class) public class MockitoExample { static abstract class TestClass { public abstract int booleanMethod(boolean arg); } @Mock TestClass testClass; @BeforeEach public void beforeEach() { when(testClass.booleanMethod(eq(true))).thenReturn(1); when(testClass.booleanMethod(eq(false))).thenReturn(2); } @Test public void test() { assertEquals(1,testClass.booleanMethod(true)); assertEquals(2,testClass.booleanMethod(false)); } }

期望是,模拟的 TestClass 显示在测试方法中测试的行为.

The expectation is, that the mocked TestClass shows the behaviour as tested in the test-method.

我得到的错误是:

org.mockito.exceptions.misusing.PotentialStubbingProblem: Strict stubbing argument mismatch. Please check: - this invocation of 'booleanMethod' method: testClass.booleanMethod(false); -> at org.oneandone.ejbcdiunit.mockito_example.MockitoExample.beforeEach(MockitoExample.java:30) - has following stubbing(s) with different arguments: 1. testClass.booleanMethod(false); -> at org.oneandone.ejbcdiunit.mockito_example.MockitoExample.beforeEach(MockitoExample.java:29) Typically, stubbing argument mismatch indicates user mistake when writing tests. Mockito fails early so that you can debug potential problem easily. However, there are legit scenarios when this exception generates false negative signal: - stubbing the same method multiple times using 'given().will()' or 'when().then()' API Please use 'will().given()' or 'doReturn().when()' API for stubbing. - stubbed method is intentionally invoked with different arguments by code under test Please use default or 'silent' JUnit Rule (equivalent of Strictness.LENIENT). For more information see javadoc for PotentialStubbingProblem class.

在这两种情况下,参数 false 似乎是匹配的,尽管我显然与 true 匹配.

In both cases, the argument false seems to be matched, though I clearly matched with true.

这是 Mockito 2.17 中的错误还是误解.我应该/如何使用 Mockito 2.x 来模拟​​具有不同布尔参数的调用?

Is that a bug in Mockito 2.17 or a misunderstanding. How should/can I use Mockito 2.x to simulate calls with different boolean arguments?

example 也可以在 github 上找到.但是surefire只会使用

The example can also be found on github. But surefire will start the test only using

mvn test -Dtest=MockitoExample

使用 Mockito 2.21 执行测试会得到相同的结果.

Executing the test using Mockito 2.21 leads to the same results.

推荐答案

从 Mockito 2.20 开始,也可以在本地添加 lenient()

Since Mockito 2.20 it is also possible, to add lenient() locally

@ExtendWith(MockitoExtension.class) public class MockitoExample { static abstract class TestClass { public abstract int booleanMethod(boolean arg); } @Mock TestClass testClass; @BeforeEach public void beforeEach() { lenient().when(testClass.booleanMethod(eq(true))).thenReturn(1); lenient().when(testClass.booleanMethod(eq(false))).thenReturn(2); } @Test public void test() { assertEquals(1,testClass.booleanMethod(true)); assertEquals(2,testClass.booleanMethod(false)); } }

更多推荐

使用 Mockito 2 模拟服务导致存根错误

本文发布于:2023-10-31 04:45:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545045.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:存根   错误   Mockito

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!