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

Ama*_*man 11 junit mocking jmock

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

Mockery context = new Mockery()
Run Code Online (Sandbox Code Playgroud)

要么

Mockery context = new JUnit4Mockery()
Run Code Online (Sandbox Code Playgroud)

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

Tob*_*oby 9

@Rhys它不是JUnit4Mockery,它取代了需要调用assertIsSatisfied,其JMock.class(与合并@RunWith).assertIsSatisfied创建常规时不需要调用Mockery.

JUnit4Mockery翻译错误.

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

Mockery context = new Mockery();
Run Code Online (Sandbox Code Playgroud)

你会得到

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?
Run Code Online (Sandbox Code Playgroud)

和使用,

Mockery context = new JUnit4Mockery();
Run Code Online (Sandbox Code Playgroud)

你会得到

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!
Run Code Online (Sandbox Code Playgroud)

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