问题 我们什么时候应该使用Mockery vs JUnit4Mockery?


如果使用JMock使用mocking编写Java单元测试,我们是否应该使用

Mockery context = new Mockery()

要么

Mockery context = new JUnit4Mockery()

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


9585
2018-05-13 08:18


起源



答案:


@Rhys不是 JUnit4Mockery 这取代了打电话的需要 assertIsSatisfied,它的 JMock.class (结合 @RunWith)。你不需要打电话 assertIsSatisfied 当你创建一个常规 Mockery

JUnit4Mockery 翻译错误。

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

Mockery context = new Mockery();

你会得到

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?

和使用,

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)而不是错误


9
2018-06-07 07:37





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

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

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

}

主要优点是无需拨打电话 assertIsSatisfied 在每次测试中,每次测试后都会自动调用。


1
2018-05-17 09:26



我认为这也是我的困惑。 - time4tea


答案:


@Rhys不是 JUnit4Mockery 这取代了打电话的需要 assertIsSatisfied,它的 JMock.class (结合 @RunWith)。你不需要打电话 assertIsSatisfied 当你创建一个常规 Mockery

JUnit4Mockery 翻译错误。

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

Mockery context = new Mockery();

你会得到

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?

和使用,

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)而不是错误


9
2018-06-07 07:37





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

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

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

}

主要优点是无需拨打电话 assertIsSatisfied 在每次测试中,每次测试后都会自动调用。


1
2018-05-17 09:26



我认为这也是我的困惑。 - time4tea


更好的是,每个 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();
     }
 }

0
2018-04-19 14:20