Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 423
Random Avatar asked Aug 03 '16 15:08

Random


People also ask

Which is best Mockito or JUnit?

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.

What is Mockito used for?

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.

How does Mockito mock work?

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.

What is Mockito JUnit example?

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.


1 Answers

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.

like image 131
GhostCat Avatar answered Sep 20 '22 11:09

GhostCat