Mockito Vs Moq

Don't be confused, the title is just to get your attention. This is not a benchmark between Mockito and Moq. I need to  explain some differences to my teammates so that the transition between Mockito and Moq is softer for them.

Typical way to stub out a method call with mockito:


Same with Moq:

colaborator.Setup(x => x.method(argument1)).Returns(whateverIwant);

Obviously, the mockito way is nicer. I believe this is because of Java capabilities but I am not sure. I would like to dig into mockito's source code to see how is this possible. I will do it and blog about it soon. I used to think that it was because of the kind ot late binding supported by Java, but I am not sure.

Moq uses a lambda expression (anonymous method inlined) to tell the framework which method and how is expected to be called). The reason I use "x" for the object in the anonymous method is because it refers to "colaborator" itself so there is no point in adding more names.

The other important difference relates to the "virtual" keyword in C#. Methods in C# are final by default. You can't override them unless they contain the keyword "virtual" on its signature. Both, Mockito and Moq create test doubles by subclassing on runtime, overriding methods and implementing the fake behavior on them (as far as I know). So, if the method you want to stub is not virtual, Moq will throw a runtime exception. If it is not virtual but you don't specify any fake behavior on it, it will not throw exceptions, but worse, it will call the actual implementation. So you might be hiting the database in your unit tests inadvertedly. What I do, is that all my methods are virtual by default (I write "virtual" on their signature).

There will be more posts on this topic 🙂

  • AnObfuscator

    I disagree, I much prefer the lambda expressions of Moq… but to each his own. 😉

    I really do hate the explicit C# virtual/override syntax. For a language so focused on syntactical sugar and eliminating unnecessary verbosity (Java, pay attention), this is a horrible inherited syntax from C++.

  • Pingback: Cleaner interaction tests « El blog de Carlos Ble()

  • SumDumGai

    I agree. I don’t think it’s “obvious” at all that Mockito is way nicer. It’s a different syntax for sure but personal preference is not an objective measure.

  • Michael

    I think you shouldn’t need to stub out concrete objects but rather decouple your classes by using interfaces. After all it is the contract you should care about not the implementation. That way you don’t need to bother about making all your methods virtual. Which is a bad idea for a lot of different reasons.

  • carlosble

    A class is also an interface. When you’ve got only one implementation, there is no point in having a separate interface with only one class implementing that interface. What name do you choose for the interface in that case ISomething? The I prefix convention for interfaces in .Net is like hungarian notation, deprecated in my opinion.

  • carlosble

    When I show Moq to someone I always have to explain what is “x” because it is not intuitive. I will starting using “self” rather than “x” to see if that helps 🙂

  • Michael

    A class defines functionality and most often exposes more methods and properties then a specific interface does.

    Using interfaces focuses on the contract and allows for better changeability and testability because classes don’t care about other classes but about providers of functionality they need. Who (which class) implements this functionality is not important. One usefull side-effect is that it is easy to create and supply mockups instead of the “real” functionality.

    A discussion of naming conventions seems to me off topic.