top of page
Writer's pictureAditi Thakkar

JUnit VS. Mockito

JUNIT:


JUnit is an open-source testing framework. To perform unit testing, we need to create test cases. The unit test case is a code which ensures that the program logic works as expected.


There are two ways to perform unit testing: 1) manual testing 2) automated testing.

  • Manual Testing - executing test cases manually without any tool support. It is time consuming and less reliable.

  • Automated Testing - executing test cases by tool support. It is fast and more reliable.


ANNOTATIONS FOR JUNIT TESTING:

  • @Test - specifies that method is the test method.

  • @Test(timeout=1000) - specifies that method will be failed if it takes longer than 1000 milliseconds (1 second).

  • @BeforeClass - specifies that method will be invoked only once, before starting all the tests.

  • @Before - specifies that method will be invoked before each test.

  • @After - specifies that method will be invoked after each test.

  • @AfterClass - specifies that method will be invoked only once, after finishing all the tests.

MOCKITO:


Mockito is a mocking framework. It is a library used to create simple and basic test APIs to perform unit tests of Java applications.


Unit testing is a software testing technique in which individual components/parts of the software is tested. This process is done during the development of an application or project. The aim of unit testing is to isolate a segment of code (unit) and verifies its correctness. A unit is referred to as an individual function or procedure (program).


Mocking is a process of developing the objects that act as the mock or clone of the real objects. It's a testing technique where mock objects are used instead of real objects for testing purposes.


Dummy output for a dummy input.


JUNIT & MOCKITO - FEATURES & DIFFERENCES:


JUnit and Mockito are widely used testing frameworks in the Java ecosystem, each serving distinct purposes.


JUnit is a unit testing framework that focuses on writing and executing test cases for individual units of code.


Mockito, on the other hand, is a mocking framework specifically designed to create mock objects for testing purposes.


DIFFERENCES:


FEATURE

JUNIT

MOCKITO

Testing Scope

JUnit is primarily used for unit testing, which involves testing individual units or components of code in isolation - behaviour and correctness of specific methods or classes.

Mockito is a mocking framework that complements JUnit by facilitating the creation of mock objects. It is commonly used in conjunction with JUnit to isolate dependencies and simulate external dependencies or collaborators during testing.

Test Case Structure

JUnit provides a structured approach to writing test cases using annotations such as @Test, @Before, and @After.

Mockito is not primarily focused on test case structure. It provides methods for creating mock objects, defining mock behaviors, and verifying interactions with those mocks during testing.

Object Mocking

JUnit does not provide built-in mocking capabilities like Mockito and focuses more on assertions and testing individual code units.

One of the key features of Mockito is its ability to create mock objects and stub or verify their behaviors. Mockito allows developers to define expectations and behaviors of mock objects.This is particularly useful when dealing with complex dependencies or external systems that are not suitable for direct testing.

Test Doubles

JUnit primarily focuses on real objects and encourages the use of test doubles, such as stubs or fakes, for isolating code units during testing. Test doubles are manually created objects that mimic the behavior of real dependencies but are simplified and tailored to specific test scenarios.

Mockito specializes in creating dynamic mock objects on the fly, allowing for more flexible and precise control over mock behavior and interaction verification.

Test Coverage

JUnit provides mechanisms to measure code coverage and assess the effectiveness of unit tests. Developers can use tools like JaCoCo or Cobertura to generate reports indicating which parts of the codebase are covered by tests.

Mockito, being a mocking framework, does not directly contribute to code coverage measurement. Its primary focus is on creating mock objects and verifying interactions with them during testing.








40 views0 comments

Recent Posts

See All

Comments


bottom of page