JMock断言在TearDown中是否满足?

发布于 2024-11-19 05:49:12 字数 440 浏览 3 评论 0原文

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

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

但是当有很多测试时,将 assertIsSatisfied 移到拆解会更好吗?

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

I don't know why, but I have always written my JMock tests like this:

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

But when there are many tests, is it better to move assertIsSatisfied to the tear-down?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

是你 2024-11-26 05:49:12

推荐的方法是使用 JMock 运行程序。使用注释该类,

@RunWith(JMock.class)
public class TestClass {

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

我们在存储库中还有一个与新的 @Rule 基础设施配合使用的嘲笑规则。

The recommended way to do this is to use the JMock runner. Annotate the class with

@RunWith(JMock.class)
public class TestClass {

This will call the assertion at the right place in the test lifecycle. Teardown isn't the right place as a failure might not be reported in correctly and might mess up other cleanup.

We also have a mockery rule in the repository that works with the new @Rule infrastructure.

还不是爱你 2024-11-26 05:49:12

是的,我倾向于在拆解中进行。通过将样板文件移至 @After 中,它将各个测试方法的重点放在实际测试的内容上 - 对我而言,测试尽可能具有表现力和可读性至关重要。

事实上,我有时会更进一步,使用 JMockSupport 基类来为我处理 Mockery (以及提供 mock( 的便捷实现) ...))。当然,这只是一种方便,绝不是像 JUnit 3 中那样的要求。

Yes, I tend to do it in teardown. It keeps the focus of the individual test methods on what they're actually testing, by removing the boilerplate out into the @After- it's critical to me that tests are as expressive and readable as possible.

In fact, I sometimes take it further than that, and use a JMockSupport base class that handles the Mockery for me (as well as providing convenience implementations of mock(...)). This is just a convenience, of course, and by no means a requirement like it was in JUnit 3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文