我们什么时候应该使用 Mockery 和 JUnit4Mockery?
如果使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Rhys这不是
JUnit4Mockery
取代了调用assertIsSatisfied
的需要,它是JMock.class
(与@RunWith< /代码>)。创建常规
Mockery
时,您不需要调用assertIsSatisfied
。JUnit4Mockery
转换错误。默认情况下,期望异常在 Junit 中报告为
ExpectationError
,因此,例如,使用you'll get
和 using,
你将得到
JUnit4Mockery 将 ExpectationError 转换为 JUnit 处理的 java.lang.AssertionError和。最终结果是,它将在您的 JUnit 报告中显示为失败(使用 JUnit4Mockery)而不是错误。
@Rhys It's not the
JUnit4Mockery
that replaces the need to callassertIsSatisfied
, its theJMock.class
(combined with the@RunWith
). You wont need to callassertIsSatisfied
when you create a regularMockery
.The
JUnit4Mockery
translates errors.By default, expectation exceptions are reported in Junit as
ExpectationError
, so for example, usingyou'll get
and using,
you'll get
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.
将 JMock 与 JUnit 4 一起使用时,您可以通过利用 JMock 测试运行程序来避免一些样板代码。执行此操作时,必须使用 JUnit4Mockery 而不是常规 Mockery。
以下是构建 JUnit 4 测试的方式:
主要优点是无需在每个测试中调用
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:
The main advantage is there is no need to call
assertIsSatisfied
in each test, it is called automatically after each test.更好的是,根据 http://incubator .apache.org/isis/core/testsupport/apidocs/org/jmock/integration/junit4/JUnitRuleMockery.html 使用@Rule并避免@RunWith,您可能需要其他系统:
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: