java - JMock assertIsSatisfied 在 TearDown 中?

标签 java unit-testing junit jmock

我不知道为什么,但我总是这样编写我的 JMock 测试:

@Test
public void testMyThing() throws Exception {
    mockery.checking(new Expectations() {{
        oneOf(mockObj).foo();
    }});
    testObj.bar(); // calls mockObj.foo()
    mockery.assertIsSatisfied();
}

但是当测试很多的时候,是不是把assertIsSatisfied移到tear-down上比较好?

@After
public void tearDown() throws Exception {
    mockery.assertIsSatisfied();
}

最佳答案

推荐的方法是使用 JMock runner。用

注释类
@RunWith(JMock.class)
public class TestClass {

这将在测试生命周期的正确位置调用断言。拆解不是正确的地方,因为可能无法正确报告故障,并且可能会扰乱其他清理工作。

我们在与新的@Rule 基础架构一起工作的存储库中还有一个模拟规则。

关于java - JMock assertIsSatisfied 在 TearDown 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6624709/

相关文章:

eclipse - 如何在 Eclipse 中创建 stub Junit 测试?

java - 在 JUnit 测试中处理 System.exit(0)

java - 在测试中使用 Spring JpaRepository

java - Hadoop jar:未知程序

java - 如何存储文件字节并稍后再次创建该文件?

c# - NUnit 示例代码?

android - 使用本地 JSON 的 Retrofit 2.0 Android 单元测试

java - 在 Magnolia 中使用模型

c# - WCF 无法解析 Java Soap 响应

unit-testing - VS 2013 MSTest 与 nUnit 与 xUnit