Sub topic 3
Your First JUnit Test
In the last chapter you added JUnit 5 to your project. Now you will write a real test. A test is a small piece of code that runs your program code and checks that it gave the right answer. JUnit runs the test for you and reports whether it passed or failed.
The Code Under Test
The code under test is the code you want to check. Here it is a tiny calculator. Save it as src/main/java/Calculator.java.
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int divide(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException("divisor must not be zero");
}
return a / b;
}
}This chapter tests only the add method. Later chapters cover divide and its exception.
Where Test Files Go
Maven and Gradle keep tests apart from program code. Tests go in a separate folder, and they are never shipped with your app.
| Folder | What goes there |
|---|---|
| `src/main/java` | Your program code, such as `Calculator.java` |
| `src/test/java` | Your tests, such as `CalculatorTest.java` |
By convention, the test class has the same name as the class it tests, plus Test at the end.
Writing the Test
Save this as src/test/java/CalculatorTest.java.
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class CalculatorTest {
@Test
void addReturnsSum() {
Calculator calculator = new Calculator();
int result = calculator.add(2, 3);
assertEquals(5, result);
}
}Here is what each part does:
import org.junit.jupiter.api.Test;brings in the@Testannotation. An annotation is a label that starts with@and gives extra information about code.@Testmarks the method as a test. JUnit looks for this label and runs every method that has it.void addReturnsSum()is the test method. It returns nothing and takes no arguments. The class and method do not need to bepublic.assertEquals(5, result)is an assertion, which is a check. It says "I expect 5, andresultshould equal it." The expected value always comes first.
What Happens When It Runs
If add were broken and returned 6, JUnit would report something like expected: <5> but was: <6>. That message shows you right away what went wrong.
Watch It Fail
Check that your test can actually catch a bug. Change add to return a - b; and run the test again. It should fail. Then put the + back. A test that has never failed might not be checking anything.
Exercise
Write a second test method in CalculatorTest named addHandlesNegativeNumbers that checks that adding -2 and -3 gives -5.
Show answer
Add this method inside the CalculatorTest class: @Test void addHandlesNegativeNumbers() { Calculator calculator = new Calculator(); int result = calculator.add(-2, -3); assertEquals(-5, result); }
Exercise
You write a method that calls calculator.add(2, 3) and assertEquals(5, result), but you forget the @Test annotation. What happens when you run the tests?
Show answer
JUnit skips the method because only methods marked with @Test are run as tests. It does not report the method as passed or failed.