Unit Testing In Java

Nikhil Bhatnagar
codeburst
Published in
4 min readApr 4, 2021

--

Testing refers to the checking of code and its functionality in different conditions. The conditions could be extreme or mild, depending upon the developer but a good code or application is one that can be handle different user scenarios and can work under any and every circumstance. The idea of testing comes from Test-Driven Development where we write tests continuously for our code, check it for different conditions, and modify our code to handle the exception cases.

Testing can be of many types like integration testing, unit testing, alpha testing, regression testing, etc but the most commonly used is unit testing which refers to the testing of code in small modules or units. It’s basically the testing of functions or classes which make up our code. As a developer, one must only focus on Unit testing.

Different programming languages and frameworks come with different testing libraries or tools. Java comes with libraries like Junit, mockito, etc which facilitates a developer for unit testing.

Junit and it’s working

Junit is a java library that can be easily imported into any project by copying the required dependency from the link here. To set up a project:-

  1. Open any IDE. Go to new project > maven project. Do the required formalities to set up a project.
  2. Go to pom.xml file and add the dependency inside <dependency></dependency> tag.
  3. Create a class inside src which has your code and your logic. (Eg:- a calculator class doing calculations here)
  4. Now, create a class(name it as the class that you want to check) for testing (Eg:- CalculatorTest). Inside the class, create a method (here, CalculatorAddTest) and annotate it with @ Test. The @ test signifies that the particular method which is tested for unit test.
  5. Inside our test method, we check for the logic of our code. Suppose a method add(int a, int b) in our above Calculator class returns 5 on adding 2 and 3 which are passed as arguments to the add method. To write a unit test for the same:-

· Declare a variable of the class (here Calculator) which has the method we want to test. Make this declaration as outside any method as we can further use this variable for checking different methods.

· Inside the method annotated with @Test, do

> when().thenReturn() are methods provided by JUnit which actually tells the compiler about the function we want to test and about the value which it should return. Here we tell the compiler that we are testing add(2,3) method of the class given by calc variable. (calc is a variable of calculator class).

> thenReturns() tells the compiler that the add() method will return 5 on call. This method tells the compiler our correct estimated value that should get returned on the call of add() method.

> assertEquals() method does the actual checking of the functionality of the method and returns true or false depending upon the test of our function. The syntax for the same is:-

assertEquals(expected true value, value we want to check)

> Now run the method or the class. Our test class would automatically return green or red ticks (or any other visual indications) showing the success or failure of our tests.

As this example is pretty simple, the significance of when().thenReturn() might not be very clear but it’s very useful when we come to deal with nested functional calls or API calls or interface calls etc.

6. Apart from the above-shown methods, Junit also gives methods like @Before (used to initialize an object or variable, etc before actual test call), @After (used to free a resource, etc after the test is over), etc. These annotations are pretty self explanatory and can be easily used after reading the JUnit documentation.

Although it’s a simple article but I hope it provides some insights as to how to get started with unit testing. But ending the article, let’s think about a scenario where our application uses a Database or a third-party API? in such cases, calls to API can be expensive and we can’t make such calls oftenly. Further, a Database gets populated by users, and practically we won’t be having a database during testing to test our methods.

In such cases, we need to mock the functionality of these services which is very easy and it uses an additional library for the same.

Thank You!

--

--

A tech enthusiast who loves to read and write about new technologies and trends. Software engineer @HashedinByDeloitte