1. Overview

JUnit is the primary choice for unit testing in Java. During the test execution, developers often face a strange error that says there are no runnable methods even when we’ve imported the correct classes.

In this tutorial, we’ll see some specific cases resulting in this error and how to fix them.

2. Missing @Test Annotation

First, the test engine must recognize the test class to execute the tests. If there are no valid tests to run, we’ll get an exception:

java.lang.Exception: No runnable methods

To avoid this, we need to ensure that the test class is always annotated with the @Test annotation from the JUnit library.

For JUnit 4.x, we should use:

import org.junit.Test;

On the other hand, if our testing library is JUnit 5.x, we should import the packages from JUnit Jupiter:

import org.junit.jupiter.api.Test;

In addition, we should pay special attention to the TestNG framework’s @Test annotation:

import org.testng.annotations.Test;

When we import this class in place of JUnit’s @Test annotation, it can cause a “no runnable methods” error.

3. Mixing JUnit 4 and JUnit 5

Some legacy projects may have both JUnit 4 and JUnit 5 libraries in the classpath. Though the compiler won’t report any errors when we mix both libraries, we may face the “no runnable methods” error when running the JUnit. Let’s look at some cases.

3.1. Wrong JUnit 4 Imports

This happens mostly due to the auto-import feature of IDEs that imports the first matching class. Let’s look at the right JUnit 4 imports:

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

As seen above, we can notice that the org.junit package contains the core classes of JUnit 4. 

3.2. Wrong JUnit 5 Imports

Similarly, we may import the JUnit 4 classes by mistake instead of JUnit 5 and tests wouldn’t run. So, we need to ensure that we import the right classes for JUnit 5:

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

Here, we can see that the core classes of JUnit 5 belongs to org.junit.jupiter.api package.

3.3. @RunWith and @ExtendWith Annotations

For projects that use Spring Framework, we need to import a special annotation for integration tests. For JUnit 4, we use the @RunWith annotation to load the Spring TestContext Framework. 

However, for the tests written on JUnit 5, we should use the @ExtendWith annotation to get the same behaviour. When we interchange these two annotations with different JUnit versions, the test engine may not find these tests.

In addition, to execute JUnit 5 tests for Spring Boot based applications, we can use the @SpringBootTest annotation that provides additional features on top of @ExtendWith annotation.

4. Test Utility Classes

Utility classes are useful when we want to reuse the same code across different classes. Accordingly, the test utility classes or parent classes share some common setup or initialization methods. Due to the class naming the test engine recognizes these classes as real test classes and tries to find testable methods.

Let’s have a look at a utility class:

public class NameUtilTest {
    public String formatName(String name) {
        return (name == null) ? name : name.replace("$", "_");
    }
}

In this case, we can observe that the NameUtilTest class matches the naming convention for the real test classes. However, there are no methods annotated with @Test which results in a “no runnable methods” error. To avoid this scenario, we can reconsider the naming of these utility classes.

As such, the utility classes that end with a “*Test” can be renamed as “*TestHelper” or similar:

public class NameUtilTestHelper {
    public String formatName(String name) {
        return (name == null) ? name : name.replace("$", "_");
    }
}

Alternatively, we can specify the abstract modifier for parent classes that end with the “Test” pattern (e.g., BaseTest) to prevent the class from test execution.

5. Explicitly Ignored Tests

Though not a common scenario, sometimes all the test methods or the entire test class could have been incorrectly marked skippable.

The @Ignore (JUnit 4) and @Disabled (JUnit 5) annotations can be useful to temporarily prevent certain tests from running. This could be a quick fix to get the build back on track when the test fix is complex or we need urgent deployments:

public class JUnit4IgnoreUnitTest {
    @Ignore
    @Test
    public void whenMethodIsIgnored_thenTestsDoNotRun() {
        Assert.assertTrue(true);
    }
}

In the above case, the enclosing JUnit4IgnoreUnitTest class has just one method and that was marked as @Ignore. When we run tests, either with an IDE or a Maven build, this might result in a “no runnable methods” error as there’s no testable method for the Test class.

To avoid this error, it’s better to remove the @Ignore annotation or have at least one valid test method for execution.

6. Conclusion

In this article, we’ve seen a few cases where we’d get a “no runnable methods” error while running tests in JUnit and how to fix each case.

Firstly, we saw that missing the right @Test annotation can cause this error. Secondly, we learned that mixing classes from JUnit 4 and JUnit 5 can lead to the same situation. We also observed the best way to name test utility classes. Finally, we discussed explicitly ignored tests and how they can be an issue.

As always, the code presented in this article is available over on GitHub.