unit-testing - EasyMock 和 Mockito 的区别

标签 unit-testing testing mockito easymock

<分区>

我正在学习使用 EasyMock 和 Mockito 进行测试。它们之间有什么区别? 它们的优点和缺点是什么?哪个更好用?

最佳答案

虽然这个问题主要是基于意见,但你从 here 中得到了不同之处。 :

Differences:

  • no record/replay modes - no need for them. There only 2 things you can do with Mockito mocks - verify or stub. Stubbing goes before execution and verification afterwards.
  • all mocks are 'nice' (even somehow nicer, because collection-returning methods return empty collections instead of nulls). Even though mocks are nice, you can verify them as strictly
    as you want and detect any unwanted interaction.
  • explicit language for better readability: verify() and when() VS the mixture of expect(mock.foo()) and mock.foo() (plain method call without 'expect'). I'm sure some of you will find this argument subjective :)
  • simplified stubbing model - stubbed methods replay all the time with stubbed value no matter how many times they are called. Works exactly like EasyMock's andStubReturn(), andStubThrow(). Also, you can stub with different return values for different arguments (like in EasyMock).
  • Verification of stubbed methods is optional because usually it's more important to test if the stubbed value is used correctly rather than where's it come from.
  • verification is explicit - verification errors point at line of code showing what interaction failed. verification in order is flexible and doesn't require to verify every single interaction.
  • custom argument matchers use hamcrest matchers, so you can use your existing hamcrest matchers. (EasyMock can also integrate with hamcrest though it is not a part of EasyMock but hamcrest. See the
    documentation of hamcrest).

关于unit-testing - EasyMock 和 Mockito 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19890648/

相关文章:

c++ - unitest 文件(visual studio)上未解析的外部符号

javascript - 测试组件快照,获取 json 错误

java - Mockito 在使用@Mock 时将空值注入(inject) Spring bean?

performance - 使用 Azure VM 作为测试代理来驱动对 Azure 云外部站点的负载测试

java - 如何设置 Mockito 模拟以对多个不同的方法调用使用相同的答案

java - JUnit 和 Mockito 的分支覆盖率

java - Mockito:使用有界通配符返回类型的 stub 方法

c# - 测试采用 IQueryable 参数的公共(public)类方法

angular - 测试ActivatedRoute.paramMap而不模拟路由器

java - 测试字符串 Junit