4

Would anyone suggest a Mock library and provide the reasoning behind the pick?

I am looking to introduce one to the existing code base.

Thanks.

Candy Chiu
  • 6,579
  • 9
  • 48
  • 69

4 Answers4

10

This is the best comparison I've seen, of multiple Java mocking frameworks, including EasyMock and mockito. [The original page is offline; this link is to an archived copy.]

It includes EasyMock, Mockito, jMock, SevenMock, JMockit, rMock, Unitils.

I used EasyMock first, but have moved to Mockito. Unit tests created with EasyMock were sometimes fragile and hard to debug. Mockito is easy, partly because it distinguishes between stubbing and verification. Here's a Mockito-oriented comparison of it and EasyMock: http://code.google.com/p/mockito/wiki/MockitoVSEasyMock.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • 1
    That comparison is very outdated. For example, the version of JMockit it evaluates is almost three years old now. The author actually promised to update it, but never came through. – Rogério Oct 11 '10 at 20:12
  • @Rogerio - You should disclose that you are the author of JMockit. – Andy Thomas Oct 11 '10 at 20:36
  • 3
    It is disclosed. My SO profile contains a link to the JMockit project site, which in turn reveals my full name. I simply find it annoying and redundant to include an explicit note about that in every post I make concerning JMockit. – Rogério Oct 12 '10 at 00:36
  • A up-to-date comparison matrix is available here, possibly biased toward JMockit http://code.google.com/p/jmockit/wiki/MockingToolkitComparisonMatrix – Damien Oct 06 '12 at 03:12
  • First link is broken. (http://www.sizovpoint.com/2009/03/java-mock-frameworks-comparison.html) – mateuscpf Mar 02 '17 at 19:55
  • @mateuscpf - Thanks for noting that. I've redirected the link to an archived copy. That said, six years after this answer, the initial comparison is now eight years old. A more contemporary comparison is desirable. – Andy Thomas Mar 02 '17 at 20:33
1

My suggestion is JMockit (I wrote it). The project site has a lot of information (and hundreds of actual JUnit tests) comparing several Java mocking APIs (EasyMock, jMock, JMockit, Mockito, PowerMock, Unitils Mock), as well as an extensive feature comparison matrix.

Rogério
  • 16,171
  • 2
  • 50
  • 63
  • 1
    It is misleading to recommend your own product without disclosing yourself as the author. – Andy Thomas Oct 11 '10 at 20:44
  • 2
    Hmm, I don't know... why do you consider it misleading, exactly? Anyone can easily discover that by checking my name here and on the project site; my identity has never been hidden. (I am not saying you're wrong, necessarily; just trying to better understand the point of view.) – Rogério Oct 12 '10 at 00:31
  • 4
    Most people do not read your profile. There is one author and n users. In the absence of other information, it is most likely that a recommendation is coming from a user. It is ethical to disclose openly that you have a conflict of interest. This is why, for example, you see ethical news media including disclosures within news stories, rather than somewhere else that some subset of readers may stumble across ... like, say ... in an unused lavatory with a sign that says "beware of the tiger." – Andy Thomas Oct 12 '10 at 17:52
  • @Rogerio, Does JMockit support TestNG? – Candy Chiu Nov 08 '10 at 07:29
  • @Candy: Yes, but only versions 5.9-5.11 for now. – Rogério Nov 08 '10 at 11:06
  • @Rogerio, Does your feature matrix base on the latest version of all the libraries? – Candy Chiu Nov 08 '10 at 22:02
  • @Candy: Yes, at this date (Nov 9, 2010) it is up-to-date, to the best of my knowledge. – Rogério Nov 09 '10 at 11:15
  • Just stating "(I wrote it)" when you suggest JMockit would suffice. Personally I think that this actually would benefit your answer. – Thorbjørn Ravn Andersen Apr 29 '13 at 11:45
0

easymock. Reasons, from their site

'EasyMock provides Mock Objects for interfaces (and objects through the class extension) by generating them on the fly using Java's proxy mechanism. Due to EasyMock's unique style of recording expectations, most refactorings will not affect the Mock Objects. So EasyMock is a perfect fit for Test-Driven Development.'

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236