The @Test annotation in JUnit marks a method as a test case. The test runner discovers every method carrying it, runs each one in isolation on a fresh instance of the test class, and reports it as passed, failed or skipped.
In current JUnit (JUnit 5, called JUnit Jupiter, and its successor line JUnit 6) the annotation is org.junit.jupiter.api.Test. The older JUnit 4 annotation org.junit.Test works differently, so check your import before anything else.
A test method in JUnit Jupiter does not need to be public. It must not be private or static, and it must not return a value (it returns void). The runner reports the test as passed when the method completes without throwing, and as failed when an assertion error or another exception escapes.
For every @Test method JUnit Jupiter, by default:
@BeforeEach.@AfterEach.@BeforeAll and @AfterAll run once per class and, by default, must be static. Test order is deterministic but intentionally not obvious, so tests must never depend on each other. If you need an order for an unavoidable reason, use @TestMethodOrder.
In JUnit 4 you could write @Test(expected = IllegalArgumentException.class) or @Test(timeout = 1000). JUnit Jupiter's @Test takes no attributes. Use assertions instead:
assertThrows returns the exception, so you can inspect its message or cause. Annotation based timeouts also exist in Jupiter as @Timeout.
| Annotation | Purpose |
|---|---|
@DisplayName("...") | Readable name shown in reports |
@Disabled("reason") | Skips a test and records the reason |
@ParameterizedTest with @ValueSource, @CsvSource, @MethodSource | Runs the same test with many inputs; replaces @Test on that method |
@RepeatedTest(n) | Runs a test several times |
@Nested | Groups tests in an inner class |
@Tag("slow") | Labels tests for filtering in builds |
@Timeout | Fails a test that runs longer than the limit |
A parameterised test uses @ParameterizedTest instead of @Test; putting both on one method is an error.
| Aspect | JUnit 4 | JUnit 5 (Jupiter) and later |
|---|---|---|
| Import | org.junit.Test | org.junit.jupiter.api.Test |
| Visibility | Test methods must be public | Package-private is fine |
| Expected exception | @Test(expected = ...) | assertThrows |
| Timeout | @Test(timeout = ...) | @Timeout or assertTimeout |
| Setup | @Before, @After | @BeforeEach, @AfterEach |
| Extension model | Runners and rules | Extensions via @ExtendWith |
JUnit 6 keeps the Jupiter programming model and the same @Test annotation, and requires Java 17 or later. Existing Jupiter tests generally migrate by updating the dependency version. Mixing a Jupiter import with a JUnit 4 runner is the classic cause of tests that silently do not run, so confirm the import and that your build uses the JUnit Platform (for example useJUnitPlatform() in Gradle).
Usually the wrong import (org.junit.Test with a Jupiter setup), a private or static method, a non-void return type, or a build tool not configured to use the JUnit Platform.
Not by itself. Jupiter can inject some parameters such as TestInfo through parameter resolvers. For data driven tests use @ParameterizedTest.
No. By default a new instance of the test class is created for each test method. You can change this with @TestInstance(Lifecycle.PER_CLASS).
Use assertThrows(ExpectedType.class, () -> code()). It replaces the expected attribute from JUnit 4.
@Test marks a method for the JUnit runner to execute. In JUnit Jupiter the method is a void, non-private, non-static method on a class that gets a fresh instance per test. Check exceptions with assertThrows, set timeouts with @Timeout, use @ParameterizedTest for many inputs, and make sure you import from org.junit.jupiter.api rather than JUnit 4.