MOCKITO: What is it and how is it different from Junit

JUnit is a framework that helps with writing and running your unit tests.

Mockito (or any other mocking tool) is a framework that you specifically use to efficiently write certain kind of tests. At it’s core, any mocking framework allows you omit instantiating “real” objects of production classes, instead the mocking framework creates a stub for you. Doing so gives you full control over that mocked object and enables you to verify the interactions taking place for example.

Having said that: one core aspect in unit testing is the fact that you want to isolate your “class under test” from anything else in the world. In order to do that, you very often have to create “test doubles” that you provide to an object of your “class under test”. You could create all those “test doubles” manually; or you use a mocking framework that generates object of a certain class for you using reflection techniques. Interestingly enough, some people advocate to never use mocking frameworks; but honestly: I can’t imagine doing that.

In other words: you can definitely use JUnit without using a mocking framework. Same is true for the reverse direction; but in reality, there are not many good reasons why you would want to use Mockito for anything else but unit testing.

Leave a Comment