How to Fix 'Test Not Found' Error in JUnit Tests
@Test annotation or has an incorrect method signature. To fix it, ensure your test methods are public, void, have no parameters, and are annotated with @Test.Why This Happens
This error occurs when JUnit cannot detect your test method. Common reasons include missing the @Test annotation, using wrong method signatures (like having parameters or returning a value), or incorrect method visibility (not public).
import org.junit.jupiter.api.Test; public class CalculatorTest { // Missing @Test annotation public void addNumbers() { // test code } // Incorrect method signature: has parameter @Test public void subtractNumbers(int a, int b) { // test code } // Incorrect method signature: returns int @Test public int multiplyNumbers() { return 0; } }
The Fix
To fix the error, add the @Test annotation to each test method. Make sure each test method is public, returns void, and takes no parameters. This allows JUnit to find and run your tests correctly.
import org.junit.jupiter.api.Test; public class CalculatorTest { @Test public void addNumbers() { // test code } @Test public void subtractNumbers() { // test code } @Test public void multiplyNumbers() { // test code } }
Prevention
Always annotate test methods with @Test and keep their signature as public void methodName() with no parameters. Use your IDE's JUnit test templates to avoid mistakes. Running tests frequently helps catch missing annotations early.
Enable linting or static analysis tools that warn about missing @Test annotations or incorrect test method signatures.
Related Errors
- Test class not found: Happens if the test class is not public or not in the test source folder.
- Method not runnable: Occurs if test methods throw checked exceptions without declaration or have invalid signatures.
- Annotation not recognized: Using JUnit 4 annotations in JUnit 5 without proper dependencies causes tests to be ignored.