Overview of Testing in Java with JUnit

Hey everyone in this article, we will quickly discuss about the Unit testing Java JUnit.

Read More:

What are Unit tests?

Unit tests are a small piece of code that tests the java methods or components. Unit tests will typically check whether the given method returns the correct result or not.

Sample Unit test

Below is the Java method which adds the 2 integer numbers and returns the result.

public class SampleTest {
	public int add(int a, int b) {
		return a + b;

Below is the Unit test case for the above method which checks whether the method add() will return the correct result or not when we pass 2 numbers.

class TestSampleTest {

	void should_ReturnSumOfTwoNumbers() {
		SampleTest sTest = new SampleTest();
		assertEquals(5, sTest.add(2, 3));


Are Unit tests necessary?

The answer is Yes. Every software should have Unit tests. Every software developer should write Unit tests for their code. Unit tests increase confidence during development. It will also help us to write more reusable and cleaner code.

If you are not able to write Unit tests for your class then chances are you have designed your class correctly.

By writing Unit tests we can detect the bugs/mistakes early in the development rather than finding them in the production.

Benefits of Unit Testing

  • Automated tests
  • Better code design
  • Fewer bugs and higher reliability
  • It helps in continuous integration/continuous deployment (CI/CD)

Who writes Unit tests?

Typically software developers write Unit tests while creating the features/modules but not software testers. Software testers will test the Application from the Customer point of view. Basically they test User interfaces and end to end testing.

What are the different types of tests are there?

There are 3 types of tests are there –

  • UI testing
  • Functional/Integration testing
  • Unit testing

UI testing: As we discussed earlier, UI testing is done by software testers. They most test the software from the customer point of view.

Functional/Integration testing: Sometimes functional testing is done by both software developers  as well as software testers. Basically testing the multiple components together as a part of the test plan. It basically checks any negative side effects due to the integration.

Unit testing: It is purely done by software developers. They write unit tests for their classes, methods etc.

What are the different Testing Framework in Java?

In the world of Java, JUnit is the de facto standard for unit testing in Java. JUnit supports creating test cases, automation of the test cases, Utilities for test setup, teardown, and assertions.

Typically this JUnit is integrated with other testing frameworks as well such as Mockito for more complex projects. Mockito helps us to create mocks and stubs. It minimizes the dependencies on external components.

At the time of writing this article, the latest version of JUnit is JUnit 5, it was released in Sep 2017.

That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply