30

I am very new to mocking framework, and my work needs mocking framework to finish unit testing. In the current code base i could see above 3 frameworks are being used in different places for unit testing. So, which one should i go for in the above 3 frameworks ?

keya
  • 2,068
  • 2
  • 18
  • 18

3 Answers3

55

I give you an explanation that other people probably wont like, but I (and a lot of people I mentioned it to) find/found very helpful: PowerMock is the mocking framework ... that you better not use.

The main advantage from PowerMock is that you can use it to test certain constructs (for example static method invocations) that EasyMock can't mock. Thus: when you want to test 3rd party code that you can't change; and that contains static calls; then you might turn to PowerMock.

But when you write your own code, and you focus on writing testable code; then you will find that the "need to use PowerMock" is absolutely equal to "you did a bad job designing your code". Using static for example directly leads to direct coupling between your classes; and therefore it is hard to get rid of it once it is in.

Don't get me wrong - PowerMock has its place in testing; but its powerful features come at certain cost.

On EasyMock / Mockito: mainly "two different ways" of writing down test cases; the later one leading to tests that many people find easier to read; whereas EasyMock and "strict" mocks allow for writing down test cases that will break quickly upon most changes in your production code (which by itself can be very useful, or very annoying).

Olmstov
  • 563
  • 7
  • 18
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    This is indeed a very good advice. Thanks for sharing your opinion! – Arefe Oct 16 '17 at 03:26
  • 1
    Great suggestion. Thanks a lot ! – keya Dec 07 '17 at 10:15
  • Would it be correct to say that private static final should only be used for: 1. Constants that should not be public (e.g. a username or password to an in-memory embedded DB) or 2. a concurrent/paralleled/thread-safe construct (e.g. ConcurrentHashMap for a simple custom cache)? – cody.tv.weber Dec 03 '19 at 13:59
  • 2
    @cody.tv.weber I would never put a password into code. doesnt belong there, normally. And well, it is for private constants. Thing that you use without your class, but no other class needs. For example: a common tracer/logger object. – GhostCat Dec 03 '19 at 14:07
4

Here you can find a comparision between Mockito and EasyMock:

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).

PowerMock is an extension of other Mocking frameworks like Mockito or EasyMock that comes with more powerful capabilities. It means that you can combine Mockito/EasyMock and PowerMock into the same unit test.

I personally use Mockito for unit testing big part of my code and PowerMock only in code that needs its extra features as testing static methods.

Julieta
  • 407
  • 2
  • 8
2

If you control your design and you should be mostly ok with Mockito. For example PowerMock allows you to mock a private member which looks cool, but is not necessary if you refactor that private member to a parameter provided through dependency injection.

Florin Grigoriu
  • 169
  • 1
  • 3