0
0
JunitDebug / FixBeginner · 4 min read

How to Fix 'Test Not Found' Error in JUnit Tests

The 'test not found' error in JUnit usually happens because the test method lacks the @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).

java
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;
    }
}
Output
No tests found for given includes: [CalculatorTest]
🔧

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.

java
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
    }
}
Output
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
🛡️

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.

Key Takeaways

Always annotate test methods with @Test for JUnit to detect them.
Test methods must be public, void, and parameterless.
Use IDE templates and run tests often to catch missing annotations early.
Keep test classes public and in the correct source folder.
Match JUnit version and annotations to avoid ignored tests.