- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在编写新的 jUnit4 测试时,我想知道是使用 @RunWith(MockitoJUnitRunner.class)
还是 MockitoAnnotations.initMocks(this)
。
我创建了一个新测试,向导使用 Runner 自动生成了一个测试。 MockitoJUnitRunner 的 Javadocs 声明如下:
Compatible with JUnit 4.4 and higher, this runner adds following behavior:
Initializes mocks annotated with Mock, so that explicit usage of MockitoAnnotations.initMocks(Object) is not necessary. Mocks are initialized before each test method.Validates framework usage after each test method.
我不清楚使用 Runner 是否比我过去使用的 initMocks()
方法有任何优势。
最佳答案
MockitoJUnitRunner
为您提供框架使用的自动验证,以及自动 initMocks()
。
框架使用的自动验证确实值得拥有。如果您犯了这些错误之一,它会为您提供更好的报告。
您调用静态 when
方法,但未使用匹配的 thenReturn
、thenThrow
或 完成 stub >然后
。 (下面代码中的错误 1)
您在模拟上调用 verify
,但忘记提供方法调用您正在尝试验证。 (下面代码中的错误 2)
您在 doReturn
、doThrow
或doAnswer
并传递一个模拟,但忘记提供该方法你正在尝试 stub 。 (下面代码中的错误 3)
如果您没有对框架使用情况进行验证,则在以下调用 Mockito 方法之前不会报告这些错误。这可能是
如果它们发生在您运行的最后一个测试中(如下面的错误 3),则根本不会报告它们。
以下是每种类型的错误的外观。假设 JUnit 按此处列出的顺序运行这些测试。
@Test
public void test1() {
// ERROR 1
// This compiles and runs, but it's an invalid use of the framework because
// Mockito is still waiting to find out what it should do when myMethod is called.
// But Mockito can't report it yet, because the call to thenReturn might
// be yet to happen.
when(myMock.method1());
doSomeTestingStuff();
// ERROR 1 is reported on the following line, even though it's not the line with
// the error.
verify(myMock).method2();
}
@Test
public void test2() {
doSomeTestingStuff();
// ERROR 2
// This compiles and runs, but it's an invalid use of the framework because
// Mockito doesn't know what method call to verify. But Mockito can't report
// it yet, because the call to the method that's being verified might
// be yet to happen.
verify(myMock);
}
@Test
public void test3() {
// ERROR 2 is reported on the following line, even though it's not even in
// the same test as the error.
doReturn("Hello").when(myMock).method1();
// ERROR 3
// This compiles and runs, but it's an invalid use of the framework because
// Mockito doesn't know what method call is being stubbed. But Mockito can't
// report it yet, because the call to the method that's being stubbed might
// be yet to happen.
doReturn("World").when(myMock);
doSomeTestingStuff();
// ERROR 3 is never reported, because there are no more Mockito calls.
}
现在当我五年多前第一次写这个答案时,我写了
So I would recommend the use of the
MockitoJUnitRunner
wherever possible. However, as Tomasz Nurkiewicz has correctly pointed out, you can't use it if you need another JUnit runner, such as the Spring one.
我的建议现在已更改。自从我第一次写这个答案以来,Mockito 团队添加了一个新功能。这是一个 JUnit 规则,它执行与 MockitoJUnitRunner
完全相同的功能。但这样更好,因为它不排除其他运行者的使用。
包括
@Rule
public MockitoRule rule = MockitoJUnit.rule();
在你的测试课上。这会初始化模拟,并自动执行框架验证;就像 MockitoJUnitRunner
一样。但是现在,您也可以使用 SpringJUnit4ClassRunner
或任何其他 JUnitRunner。从 Mockito 2.1.0 开始,有更多选项可以准确控制报告的问题类型。
关于java - @RunWith(MockitoJUnitRunner.class) 与 MockitoAnnotations.initMocks(this),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10806345/
我有一个 gradle 文件 testCompile('junit:junit') testCompile('org.powermock:powermock-core:1.6.5') testComp
我使用 @RunWith(MockitoJUnitRunner.class) 与mockito 进行 junit 测试。但现在我正在使用 spring boot 应用程序并尝试使用 @RunWith(
在通常使用 @Mock 和 @InjectMocks 注释的模拟中,被测类应该使用 @RunWith(MockitoJUnitRunner.class)。 @RunWith(MockitoJUnitR
@RunWith(MockitoJUnitRunner.class) 和 @RunWith(SpringJUnit4ClassRunner.class) 有什么区别?什么时候合适使用它? 最佳答案 M
在使用 Spring Boot 时,我总是将 @SpringBootTest 添加到我的测试类中,并且我的测试按预期工作。我想知道添加 @RunWith(SpringRunner.class) 能带来
目前我的测试类有以下内容: @RunWith(Parameterized.class) @RunWith(PowerMockRunner.class) public class TestApp ext
我的项目是用 JUnit 5 设置的,我想为我的测试使用不同的 Runner,但我什至不能使用 @RunWith 注释,因为它无法编译。在本指南中,https://www.baeldung.com/j
在不研究 JUnit 源代码本身(我的下一步)的情况下,是否有一种简单的方法可以设置每个测试使用的默认 Runner,而不必在每个测试上设置 @RunWith?我们有大量的单元测试,我希望能够全面添加
我正在从仅使用 Intellij 管理我的构建系统转向使用 Intellij/Maven。当我通过@RunWith(KmlParameterizedRunner.class) 使用我自己的运行器运行我
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 7 年前。 此问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-topic在这里
我有一个自定义测试运行程序来运行部分测试,以便我可以将测试分布在不同的 jenkins 节点上。如果运行所有集成测试,则需要一个小时。所以我有 3 台服务器运行 1/3 的测试,总共只需要 20 分钟
我有注释这些注释的类: @ContextConfiguration(locations = { "classpath:pathToXml.xml" }) @RunWith(Spring
我正在尝试让 RunWith(PowerMockRunner.class) 使用我现有的包注释。 版本: powermock 1.4.12 mockito 1.9.0 junit 4.8.2 pack
我有这个非常简单的类: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath*:/
以下 Java 类无法在我的 IDE (Intellij IDEA) 中运行。 IDE 没有给出任何原因,这使得故障排除变得痛苦和困难。 这个类看起来像这样: import cucumber.api.
这个问题在这里已经有了答案: Initialising mock objects - Mockito (8 个答案) 关闭 6 年前。 我正在使用 Junit 4.8.2。当我使用 @RunWith
我有以下测试代码: package soundSystem; import static org.junit.Assert.*; import org.junit.Test; import org.j
我正在使用 spring boot starter test 编写 JUnit 测试用例。我喜欢使用 JunitParamrunner,它有助于为参数化测试传递文件。基本上它逐行读取文件中的数据,并为
我正在处理 BDD 文件并尝试使用 JUnit 进行测试。 我想将 RunCukesTest 类与 @RunWith(Cucumber.class) 一起使用。 我在很多网站上搜索过如何安装要求,但我
这个注解有什么作用? 我想什么时候使用它? 我什么时候不想使用它? @RunWith(SpringJUnit4ClassRunner.class) 当我使用 Google 搜索时,我可以找到更多的用法
我是一名优秀的程序员,十分优秀!