What is the purpose of the @Test annotation?

The @Test annotation marks a method as a test case that the JUnit framework should discover and execute automatically as part of a test run.

Key Points

Example

Annotating a method calculatesSumCorrectly() with @Test tells JUnit to run it automatically during the build, checking that a Calculator's add() method returns the right result.

@Test
void calculatesSumCorrectly() {
    assertEquals(5, calculator.add(2, 3));
}

Interview Tip

A concise interview answer is

@Test marks a method as a unit test that JUnit should discover and run automatically. It's how the framework identifies which methods to execute, track pass/fail results for, and report on, without needing a main method or manual invocation.

Last updated: 16 July 2026

Previous question: How do you structure a test case in JUnit?

Next question: How do you mock a static method in JUnit? Is it possible without external libraries?

More JUnit interview questions

All JUnit questions