I want to know what is Mockito?
Is it supporting Junit or is it an environment for writing Junit test cases?
Can some one please explain me the difference between Junit and Mockito?
JUnit and Mockito can be primarily classified as "Testing Frameworks" tools. JUnit and Mockito are both open source tools. It seems that Mockito with 9.02K GitHub stars and 1.62K forks on GitHub has more adoption than JUnit with 7.53K GitHub stars and 2.8K GitHub forks.
Mockito is a mocking framework, JAVA-based library that is used for effective unit testing of JAVA applications. Mockito is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in unit testing.
Simply put, Mockito works by storing and retrieving method invocation details on mocks using method interception. Method interception is a technique used in Aspect Oriented Programming(AOP) to address cross-cutting concerns in a software system like logging, statistics etc., by intercepting method calls.
Here, we are going to use the Mockito framework along with the JUnit framework. We can also use Spring Boot instead of using the JUnit framework. JUnit is one of the testing frameworks used by the Java programmers for creating test cases.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With