Recently, I dove into unit testing with JUnit 5 as part of my software development journey. Unit testing helps ensure that individual parts of a program work correctly by writing small, focused tests. JUnit 5 is the framework that is used to write these tests for Java applications, and it makes the process simple and efficient.
The first things first, JUnit 5 uses something called annotations to define test methods. The most important one is @Test
, which marks a method as a test case. These methods test small units of code, like individual methods in a class, to make sure they return the expected results.
Here’s a simple example of a test method I wrote to check the area of a rectangle:
import static org.junit.jupiter.api.Assertions.assertEquals;
@Test
void testRectangleArea() {
Rectangle r1 = new Rectangle(2, 3);
int area = r1.getArea();
assertEquals(6, area); // Checking if the area is correct
}
In this case try and write these small test cases to check specific outputs, and if something doesn’t match what you expect, JUnit will let you know right away.
The Structure of a Test Case
There are three simple steps you can follow for each test case:
Assert: Compare the result with what you expect using something called an “assertion.
Arrange: Set up the objects or data you are testing.
Act: Call the method you want to test.
For example, here is another test to check if a rectangle is a square:
@Test
void testRectangleNotSquare() {
Rectangle r1 = new Rectangle(2, 3);
boolean isSquare = r1.isSquare();
assertFalse(isSquare); // Checking if it’s not a square
}
In this case, using assertFalse
helps to confirm that the rectangle is not a square.
Common JUnit Assertions
JUnit 5 offers several assertion methods, and I quickly got the hang of using them. Here are a few that I used the most:
assertEquals(expected, actual)
: Checks if two values are equal.assertFalse(condition)
: Checks if a condition is false.assertTrue(condition)
: Checks if a condition is true.assertNull(object)
: Verifies if something is null.
These assertions make it easy to confirm whether a piece of code behaves as expected.
Managing Test Execution
One thing that surprised me was that test methods don’t run in any specific order by default. This means each test should be independent of the others, which encourages better organization. I also learned about lifecycle methods like @BeforeEach
and @AfterEach
, which allow you to run setup and cleanup code before and after each test case. For example, @BeforeEach
can be used to initialize objects before each test:
@BeforeEach
void setup() {
// Code to run before each test
}
In conclusion, Learning unit testing with JUnit 5 has been a great experience. It helps me write reliable code and catch bugs early. By writing small tests and using assertions, I can quickly confirm that my programs work as they should. JUnit 5 makes testing simple, and I look forward to improving my skills even more in the future!
If you’re new to testing like I was, JUnit 5 is definitely a great place to start!
From the blog CS@Worcester – MY_BLOG_ by Serah Matovu and used with permission of the author. All other rights reserved by the author.