JUnit and Mockito

Reading Time: 3 minutes

This blog will explain to you the unit testing with the Junit framework

Unit Testing

It is a software testing technique in which we test individual components/parts of the software, i.e., a group of computer programs, usage procedures, etc. We do unit testing of an object while developing an application or project. The aim of unit testing is to isolate a segment of code (unit) and verify its correctness. A unit is an individual function or procedure (program). The developers usually perform it during testing

Mocking

Mocking is a process of developing the objects that act as the mocks or clones of the real objects. In other words, mocking is a testing technique where we use mock objects instead of real objects. Mock objects provide a specific (dummy) output for a particular (dummy) input passed to it.

Need for mocking

Before using the Mocking technique, we should know the reasons for using mocking, which is as follows:

  • If we want to test a component that depends on the other component, but it is under development. It generally uses when working in a team and we divide parts between different team members. In this case, mocking plays an essential role in the testing of that component. Without mocking, we need to wait for the completion of the required elements for testing.
  • If the real components perform slow operations while dealing with database connections or another complex read/ write operation. Sometimes the database queries can take 10, 20, or more seconds to execute. In such cases, we require mock objects to perform testing, and we do it via mocking.
  • If there is an infrastructure concern that makes the testing impossible. It is very similar to the first case. For example, when we create a connection to the database, some issues related to configurations occur. It requires mocking for creating mock components to provide unit testing.

Mockito

Mockito is a Java-based mocking framework used for unit testing of Java applications. Plays a crucial role in developing testable applications. It was an open-source testing framework under the MIT (Massachusetts Institute of Technology) License. It internally uses the Java Reflection API to generate mock objects for a specific interface. We refer to mock objects as the dummy or proxy objects used for actual implementations.

The main purpose of using this framework is to simplify the development of a test by mocking external dependencies and use them in the test code. As a result, it provides a simpler test code that is easier to read, understand, and modify. We can also use it with other testing frameworks like JUnit and TestNG.

WHAT IS JUNIT?
A JUnit test is in a Test class that we use only for testing. To define a method as a test method, annotate it with the @Test annotation.
This method executes the code under test.

Annotations for JUnit Testing:
The Junit 4.x framework is annotation based, so let’s see the annotations that we use while writing the test cases:

  • @Test annotation specifies that method is the test method.
  • @Test(timeout=1000) annotation specifies that method will be failed if it takes longer than 1000 milliseconds (1 second).
  • @BeforeClass annotation specifies that method will be invoked only once, before starting all the tests.
  • @Before annotation specifies that method will be invoked before each test.
  • @After annotation specifies that method will be invoked after each test.
  • @AfterClass annotation specifies that method will be invoked only once, after finishing all the tests.

How to check the Expected results from @Test method?

  • If the method is returning something(i.e not void):
    • To check the expected result with the actual result we can use asset and other assert frameworks. These we call as typically asserts or assert statements.
    • Assert statements typically allow to define messages which shows if the test fails. You should provide here meaningful messages to make it easier for the user to identify and fix the problem.
    • Create a object of that class,assertEquals(expected, actual)
  • If the method is returning void:
    • verify(), we can be use to ensure calling of a method.

Features of JUnit:

  • JUnit is an open source framework which we use for writing and running tests.
  • Provides annotations to identify test methods.
  • Provides assertions for testing expected results.

For better understanding you can refer to below links:
https://www.tutorialspoint.com/mockito/index.htm
https://blog.knoldus.com/tag/unit-testing/

knoldus