Java > Testing in Java > Unit Testing > JUnit 5 Basics
Basic JUnit 5 Test Example
This snippet demonstrates a basic unit test using JUnit 5, showcasing the fundamental annotations and structure of a JUnit 5 test class.
Project setup
To begin, ensure you have JUnit 5 added to your project. If you're using Maven, add the following dependency to your pom.xml:
<!-- JUnit 5 Dependency (Maven) -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
Simple JUnit 5 Test Class
This code snippet shows a basic JUnit 5 test. Let's break it down:
Test
annotation from JUnit 5, which marks a method as a test case.Assertions
class, allowing you to use assertion methods like assertEquals
directly without qualifying them.testAddition
method is a test method that JUnit should execute.
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class SimpleCalculatorTest {
@Test
void testAddition() {
SimpleCalculator calculator = new SimpleCalculator();
int result = calculator.add(2, 3);
assertEquals(5, result, "Addition should return the correct sum");
}
}
SimpleCalculator Class (Code Under Test)
This is a simple class with an add
method. The test verifies that this method correctly adds two integers.
public class SimpleCalculator {
public int add(int a, int b) {
return a + b;
}
}
Concepts Behind the Snippet
The core idea behind this snippet is unit testing, where individual units (methods or classes) are tested in isolation. JUnit 5 provides a framework for writing and running these tests. The @Test
annotation marks a method as a test case, and assertion methods (like assertEquals
) verify that the code behaves as expected.
Real-Life Use Case
Imagine you're developing a banking application. You'd want to unit test the methods responsible for calculating interest, processing transactions, and applying fees to ensure they function accurately before integrating them into the larger system. This simple addition example extends to more complex scenarios like validating data, handling exceptions, and interacting with external services.
Best Practices
Interview Tip
When discussing unit testing in an interview, be prepared to explain the benefits (early bug detection, code maintainability, etc.), the difference between unit and integration testing, and your experience with testing frameworks like JUnit 5.
When to Use Them
Use JUnit 5 to write unit tests for any Java code you want to verify. It's particularly useful for complex logic, critical business rules, and code that's prone to errors.
Alternatives
While JUnit 5 is a popular choice, other testing frameworks are available, such as TestNG. Mocking frameworks like Mockito can be used in conjunction with JUnit to isolate dependencies during testing.
Pros
Cons
FAQ
-
What is the purpose of the @Test annotation?
The@Test
annotation marks a method as a test case. JUnit will execute all methods annotated with@Test
when the test suite is run. -
What is an assertion in JUnit?
An assertion is a method that verifies a specific condition. If the condition is true, the test passes; otherwise, the test fails. JUnit provides various assertion methods likeassertEquals
,assertTrue
, andassertFalse
. -
How do I run JUnit tests?
You can run JUnit tests using your IDE (e.g., IntelliJ IDEA, Eclipse), a build tool (e.g., Maven, Gradle), or the command line. Your IDE or build tool will typically have a dedicated JUnit test runner.