Sub topic 4
Running Tests and Reading the Results
You have written a test. Now you need to run it and understand what JUnit tells you. You can run tests in two places: your IDE and your build tool. An IDE (Integrated Development Environment) is a program for writing code, such as IntelliJ IDEA, Eclipse, or VS Code. A build tool is a program that compiles and tests your project, such as Maven or Gradle.
Running Tests in the IDE
Most IDEs show a small green arrow next to each test class and each test method.
- Click the arrow next to a method to run just that one test.
- Click the arrow next to a class to run every test in it.
- Right-click the test folder to run all tests in the project.
The IDE opens a test window. A green check means the test passed. A red cross or a yellow mark means it failed. Click a failed test to see its message.
Running Tests with the Build Tool
The build tool runs the same tests from the command line. Your teammates and your CI server use this method. A CI (Continuous Integration) server is a machine that builds and tests your code every time someone pushes a change.
| Task | Maven | Gradle |
|---|---|---|
| Run all tests | `mvn test` | `./gradlew test` |
| Run one class | `mvn test -Dtest=CalculatorTest` | `./gradlew test --tests CalculatorTest` |
| Run one method | `mvn test -Dtest=CalculatorTest#addReturnsSum` | `./gradlew test --tests CalculatorTest.addReturnsSum` |
| HTML report | `target/surefire-reports/` (text and XML) | `build/reports/tests/test/index.html` |
Reading a Passing Run
When every test passes, Maven prints a summary like this:
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESSGradle stays quiet when everything works. It prints BUILD SUCCESSFUL and nothing else.
Reading a Failing Run
Suppose a bug makes add(2, 3) return 6. The test fails, and you see something like this:
CalculatorTest > addReturnsSum() FAILED
org.opentest4j.AssertionFailedError: expected: <5> but was: <6>
at CalculatorTest.addReturnsSum(CalculatorTest.java:19)Read it in three parts:
- Which test failed:
addReturnsSum()inCalculatorTest. - Why it failed: the test expected
5, but the code returned6. - Where it failed: line 19 of
CalculatorTest.java. This list of code locations is called a stack trace.
JUnit sorts problems into two kinds:
- A failure means an assertion did not hold. Your code gave the wrong answer.
- An error means the test threw an exception you did not expect, such as a
NullPointerException. The test crashed before it could check anything.
Exercise
Maven reports "Tests run: 4, Failures: 1, Errors: 1, Skipped: 0". How many tests passed, and what is the difference between the two problems?
Show answer
Two tests passed. The failure is a test where an assertion did not hold, so the code returned the wrong value. The error is a test that threw an unexpected exception before its checks could finish.
Exercise
Which Gradle command runs only the divideByZeroThrows method in CalculatorTest?
Show answer
./gradlew test --tests CalculatorTest.divideByZeroThrows