The @Test annotation marks a method as a test case that the JUnit engine discovers and runs. In JUnit 5 (Jupiter) and JUnit 6 it lives in org.junit.jupiter.api.Test; in the legacy JUnit 4 it is org.junit.Test. A test method passes if it finishes without throwing an exception, and fails when an assertion fails or an unexpected exception escapes.
Always check the import. Mixing the JUnit 4 @Test with the Jupiter engine silently runs zero tests, which is one of the most common migration mistakes.
JUnit scans classes for methods annotated with @Test, creates an instance of the test class, runs lifecycle methods, and invokes the test. Each test method gets its own instance by default, so tests cannot share state through fields.
Test methods must not be private, static or return a value. In JUnit 5 and later, both the class and the method can be package-private, so public is no longer needed.
| Feature | JUnit 4 | JUnit 5 and 6 |
|---|---|---|
| Import | org.junit.Test | org.junit.jupiter.api.Test |
| Visibility | public method and class | Package-private is fine |
| Expected exception | @Test(expected = X.class) | assertThrows(X.class, ...) |
| Timeout | @Test(timeout = 1000) | @Timeout annotation or assertTimeout |
| Setup and teardown | @Before, @After | @BeforeEach, @AfterEach |
| Disable | @Ignore | @Disabled |
The JUnit 5 @Test has no attributes. Behaviour such as timeouts or conditions is added with separate annotations and extensions.
assertThrows is better than the old expected attribute because it lets you check the message and ensures only the intended call throws.
The order of test methods is deterministic but deliberately not obvious. Do not depend on it; use @TestMethodOrder only when you truly need an order.
Name tests after behaviour, such as rejectsEmptyInput, rather than test1. Follow the arrange, act, assert pattern, keep one logical assertion per test, and avoid logic such as loops or conditionals inside a test. Use @DisplayName when a sentence reads better in reports.
@ParameterizedTest runs the same method with multiple inputs from @ValueSource, @CsvSource or @MethodSource.@RepeatedTest repeats a test a set number of times.@DisplayName gives a readable name in reports.@Disabled skips a test with an optional reason.@Nested groups tests in inner classes.These replace @Test on the method; you do not combine @Test with @ParameterizedTest, because that would run the method twice and fail on missing arguments.
Usually the method uses the JUnit 4 @Test import with the Jupiter engine, is private or static, or the build is missing useJUnitPlatform() (Gradle) or a current Surefire version (Maven).
No. It must return void, otherwise JUnit will not run it.
Use assertThrows(ExpectedException.class, executable) and inspect the returned exception if you need to.
The annotation and its package stay the same for typical tests. JUnit 6 raises the minimum Java version, so check the release notes before upgrading.
@Test identifies a test method for the JUnit engine. Import it from org.junit.jupiter.api for modern code, keep methods void and non-private, use assertThrows and @Timeout instead of the removed attributes, and rely on fresh test instances plus lifecycle annotations for isolation.