我们什么时候应该使用 Mockery 和 JUnit4Mockery?

发布于 2024-09-01 12:54:05 字数 204 浏览 10 评论 0原文

如果使用 JMock 编写带有模拟的 Java 单元测试,我们应该使用

Mockery context = new Mockery()

Mockery context = new JUnit4Mockery()

两者之间有什么区别,什么时候应该使用哪个?

If writing a Java unit test with mocking using JMock, should we use

Mockery context = new Mockery()

or

Mockery context = new JUnit4Mockery()

What is the difference between the two, and when should we use which?

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

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

发布评论

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

评论(3

同尘 2024-09-08 12:54:05

@Rhys这不是JUnit4Mockery取代了调用assertIsSatisfied的需要,它是JMock.class(与@RunWith< /代码>)。创建常规 Mockery 时,您不需要调用 assertIsSatisfied

JUnit4Mockery 转换错误。

默认情况下,期望异常在 Junit 中报告为 ExpectationError,因此,例如,使用

Mockery context = new Mockery();

you'll get

unexpected invocation: bar.bar()
no expectations specified: did you...
 - forget to start an expectation with a cardinality clause?
 - call a mocked method to specify the parameter of an expectation?

和 using,

Mockery context = new JUnit4Mockery();

你将得到

java.lang.AssertionError: unexpected invocation: bar.bar()
no expectations specified: did you...
 - forget to start an expectation with a cardinality clause?
 - call a mocked method to specify the parameter of an expectation?
what happened before this: nothing!

JUnit4Mockery 将 ExpectationError 转换为 JUnit 处理的 java.lang.AssertionError和。最终结果是,它将在您的 JUnit 报告中显示为失败(使用 JUnit4Mockery)而不是错误

@Rhys It's not the JUnit4Mockery that replaces the need to call assertIsSatisfied, its the JMock.class (combined with the @RunWith). You wont need to call assertIsSatisfied when you create a regular Mockery.

The JUnit4Mockery translates errors.

By default, expectation exceptions are reported in Junit as ExpectationError, so for example, using

Mockery context = new Mockery();

you'll get

unexpected invocation: bar.bar()
no expectations specified: did you...
 - forget to start an expectation with a cardinality clause?
 - call a mocked method to specify the parameter of an expectation?

and using,

Mockery context = new JUnit4Mockery();

you'll get

java.lang.AssertionError: unexpected invocation: bar.bar()
no expectations specified: did you...
 - forget to start an expectation with a cardinality clause?
 - call a mocked method to specify the parameter of an expectation?
what happened before this: nothing!

The JUnit4Mockery converted the ExpectationError to an java.lang.AssertionError which JUnit deals with. Net result is that it'll show up in your JUnit report as an failure (using JUnit4Mockery) rather than an error.

把梦留给海 2024-09-08 12:54:05

将 JMock 与 JUnit 4 一起使用时,您可以通过利用 JMock 测试运行程序来避免一些样板代码。执行此操作时,必须使用 JUnit4Mockery 而不是常规 Mockery。

以下是构建 JUnit 4 测试的方式:

@RunWith(JMock.class)
public void SomeTest() {
  Mockery context = new JUnit4Mockery();

}

主要优点是无需在每个测试中调用 assertIsSatisfied,它会在每次测试后自动调用。

When using JMock with JUnit 4, you can avoid some boilerplate code by taking advantage of the JMock test runner. When you do this, you must use the JUnit4Mockery instead of the regular Mockery.

Here is how you'd structure a JUnit 4 test:

@RunWith(JMock.class)
public void SomeTest() {
  Mockery context = new JUnit4Mockery();

}

The main advantage is there is no need to call assertIsSatisfied in each test, it is called automatically after each test.

下雨或天晴 2024-09-08 12:54:05

更好的是,根据 http://incubator .apache.org/isis/core/testsupport/apidocs/org/jmock/integration/junit4/JUnitRuleMockery.html 使用@Rule并避免@RunWith,您可能需要其他系统:

public class ATestWithSatisfiedExpectations {
     @Rule
     public final JUnitRuleMockery context = new JUnitRuleMockery();
     private final Runnable runnable = context.mock(Runnable.class);

     @Test
     public void doesSatisfyExpectations() {
         context.checking(new Expectations() {
             {
                 oneOf(runnable).run();
             }
         });

         runnable.run();
     }
 }

Better yet, per http://incubator.apache.org/isis/core/testsupport/apidocs/org/jmock/integration/junit4/JUnitRuleMockery.html use @Rule and avoid @RunWith which you might need for some other system:

public class ATestWithSatisfiedExpectations {
     @Rule
     public final JUnitRuleMockery context = new JUnitRuleMockery();
     private final Runnable runnable = context.mock(Runnable.class);

     @Test
     public void doesSatisfyExpectations() {
         context.checking(new Expectations() {
             {
                 oneOf(runnable).run();
             }
         });

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