Basics of JUnit Testing

Matias Silva
3 min readFeb 12, 2023

--

JUnit is a widely used testing framework in Java that helps developers write and run tests for their code. Writing tests is a critical part of the software development process, as it helps to ensure that the code is working as expected and provides a way to detect bugs early in the development cycle. In this article, we will explore the importance of JUnit tests and provide examples of how to write them.

Why are JUnit Tests Important?

  1. Improved Code Quality: By writing tests, developers can catch bugs and defects early in the development process, making it easier to fix them. This results in higher-quality code that is more reliable and less prone to crashes or other issues.
  2. Faster Debugging: When tests fail, it’s easier to locate the source of the problem because you know exactly which code is causing the failure. This makes debugging faster and less frustrating for developers.
  3. Increased Confidence: Tests provide developers with the confidence that their code is working as expected. This allows developers to make changes to the code with more confidence, knowing that they can easily detect any issues that may arise.
  4. Easier Maintenance: When changes are made to the code, tests help to ensure that the changes do not break existing functionality. This makes it easier to maintain the code over time, as developers can quickly identify any issues that arise.

Examples of JUnit Tests

Testing a method that calculates the sum of two numbers:

import org.junit.Test;
import static org.junit.Assert.*;

public class TestSum {
@Test
public void testSum() {
int a = 1;
int b = 2;
int expectedResult = 3;
int result = Sum.sum(a, b);
assertEquals(expectedResult, result);
}
}

The code starts with importing the necessary JUnit classes, the “TestSum” class is then declared to contain the JUnit test cases, the “testSum” method is marked with the “@Test” annotation, indicating that it is a test method that JUnit should run.

In the “testSum” method, two variables “a” and “b” are declared and initialized to 1 and 2, respectively. Another variable “expectedResult” is declared and initialized to 3, which represents the expected result of the test, the “Sum.sum(a, b)” call invokes the “sum” method of the “Sum” class and stores the result in the “result” variable.

Finally, the “assertEquals” method is called with the “expectedResult” and “result” variables as arguments, the “assertEquals” method compares the two values and if they are not equal, the test fails, which means that the “Sum” class is not working as expected.

Here we can se another example using assertEquals:

import org.junit.Test;
import static org.junit.Assert.*;

public class TestStringUtils {
@Test
public void testIsEmpty() {
String input = "";
boolean expectedResult = true;
boolean result = StringUtils.isEmpty(input);
assertEquals(expectedResult, result);
}
}

Conclusion

JUnit tests are an essential part of the software development process, as they help to ensure that code is working as expected and make it easier to detect and fix bugs early in the development cycle. By writing tests, developers can have confidence in their code and make changes to it with less fear of introducing new bugs. JUnit has many details that you can learn, but with the examples provided in this article, you should now have a basic understanding of how to write JUnit tests in Java.

--

--