Skip to content
javahacks.dev
JUnit

Fix JUnit No Runnable Methods and Initialization Errors

Fix the JUnit "No runnable methods" and initializationError failures caused by mixing JUnit 4 and JUnit 5 annotations or a wrongly configured runner.

6 min read
Summarize in AI
Flat illustration of a log with a failing line and a wrench, with the JUnit5, OpenJDK, Spring and Gradle logos.

A test that runs in one place fails in another with:

Text
initializationError(dev.javahacks.OrderServiceTest)  Time elapsed: 0.001 s  <<< ERROR!
java.lang.Exception: No runnable methods
	at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:...)

The stack trace names BlockJUnit4ClassRunner, which is a JUnit 4 class. It looked for methods carrying org.junit.Test and found none, because your methods carry org.junit.jupiter.api.Test. Two frameworks are being mixed. The relationship of the old and new APIs is in JUnit 5 architecture.

Why the JUnit 4 runner touched a Jupiter class

Something decided to run the class with JUnit 4: a build plugin with a JUnit 4 provider, an IDE configured to use the JUnit 4 runner, or an annotation like @RunWith left on the class. All three paths end in the same message.

WhereFix
Maven Surefire older than 2.22Upgrade the plugin; see tests not running in Maven
Gradle without useJUnitPlatform()Add it to tasks.test
@RunWith(...) on the classRemove it, replace with an extension
IDE run configurationChoose "JUnit 5" or reimport the project

Replace the runner with an extension

java
// JUnit 4
@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderServiceTest { @Test public void works() { } }

// JUnit 5
@SpringBootTest
class OrderServiceTest { @Test void works() { } }

@SpringBootTest already registers the Spring extension in Spring Boot 2.1 and later. For Mockito, use @ExtendWith(MockitoExtension.class) instead of @RunWith(MockitoJUnitRunner.class). The extension model is described in the JUnit 5 handbook.

Check the imports

java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import static org.junit.jupiter.api.Assertions.assertEquals;

Search the project for the old ones:

Shell
grep -rn "import org.junit.Test\|import org.junit.Before\|import org.junit.Assert" src/test

Each hit is a JUnit 4 import. Convert them with the matching Jupiter annotation (@Before becomes @BeforeEach, @BeforeClass becomes @BeforeAll, @Ignore becomes @Disabled). OpenRewrite has a recipe that migrates JUnit 4 to 5 in bulk.

Keep both while you migrate

Some projects have hundreds of JUnit 4 tests. The Vintage engine runs them next to Jupiter:

XML
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
</dependency>

Spring Boot stopped including it by default in 2.4, so a project upgrading from older Spring Boot silently loses its JUnit 4 tests. Add the dependency back or migrate. Remember the reverse: with Vintage on the classpath, a JUnit 5 build still needs the platform enabled.

Other initialization failures that look alike

MessageCause
Test class should have exactly one public constructorJUnit 4 requires a public no-argument constructor
java.lang.Exception: Method x() should be publicJUnit 4 requires public methods; Jupiter does not
TestInstantiationExceptionJupiter could not construct the class, usually a constructor argument it cannot resolve

The last one is Jupiter's own: constructor parameters are resolved by parameter resolvers, and an unresolved type fails the class. Register the extension that supplies it.

Reproduce it with the JUnit 4 runner

You can trigger the error in ten lines, which makes the cause obvious. A test class written for Jupiter (org.junit.jupiter.api.Test) is handed to the JUnit 4 runner, JUnitCore, the way an old IDE setting or a Maven plugin configured for 4 would do.

JupiterStyleTest.javajava
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class JupiterStyleTest {
    @Test
    void adds() { assertEquals(4, 2 + 2); }
}
Run4.javajava
public class Run4 {
    public static void main(String[] a) {
        var result = org.junit.runner.JUnitCore.runClasses(JupiterStyleTest.class);
        result.getFailures().forEach(f -> System.out.println(f.getTestHeader() + " : " + f.getMessage()));
    }
}
Text
initializationError(JupiterStyleTest) : Invalid test class 'JupiterStyleTest':
  1. No runnable methods

JUnit 4 looks for methods annotated with org.junit.Test. The annotation on this method is a different type, org.junit.jupiter.api.Test, so JUnit 4 sees no test method and fails the class during initialisation, before any test runs. Nothing is wrong with the test. The wrong engine was asked to run it. That is the whole error, and it tells you where to look: not at the method, but at who is running the class.

Find who is running the class

Three places pick the engine. The IDE run configuration may be set to a JUnit 4 launcher. The build tool may be running tests through an old plugin: Maven Surefire before 2.22 does not know Jupiter and needs the provider configured, while a current one detects it. And a class carrying @RunWith(...) is explicitly a JUnit 4 class, which the Jupiter engine ignores unless the Vintage engine is present.

Shell
./mvnw -q help:effective-pom | grep -A3 'maven-surefire-plugin'
./mvnw -q dependency:tree | grep -E 'junit'

The second command is the one that usually answers. A project that lists junit:junit:4.13.2 and junit-jupiter together without junit-vintage-engine runs Jupiter tests through the platform and ignores JUnit 4 tests, while one that has only junit:junit has no Jupiter engine at all. Whichever it is, the fix is to make the engine and the annotations agree.

Make the imports agree

The most common real-world cause is a mixed import, in a file that uses the annotation from one version and a rule or assertion from another.

java
// Wrong: the Jupiter @Test with a JUnit 4 @Rule
import org.junit.jupiter.api.Test;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;

// Right: Jupiter throughout, @TempDir replaces the rule
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Shell
grep -rln 'import org.junit.jupiter' src/test | xargs grep -ln 'import org.junit\.\(Test\|Rule\|Before\|After\|runner\)'

That command lists the files that mix both. Every file it prints needs its imports unified. For rules, Jupiter provides extensions: @TempDir for TemporaryFolder, @ExtendWith(MockitoExtension.class) for MockitoJUnitRunner, and @ExtendWith(SpringExtension.class) for SpringRunner, which Spring Boot's own test annotations already include.

Run both generations during a migration

A project with thousands of JUnit 4 tests does not have to convert them at once. The Vintage engine runs JUnit 4 tests on the JUnit Platform beside Jupiter ones.

XML
<dependency>
  <groupId>org.junit.vintage</groupId>
  <artifactId>junit-vintage-engine</artifactId>
  <scope>test</scope>
</dependency>

With both engines present, a class is run by the engine that recognises its annotations, and a count of tests before and after the change tells you whether any class went unrecognised. Convert in the order of change frequency, so the tests you touch most are the first to use the new model, and remove the Vintage engine when the last @RunWith is gone.

A checklist for a class that will not start

When the message appears, five checks in order find the cause almost every time. First, read the import of @Test in the class: org.junit.jupiter.api.Test is Jupiter and org.junit.Test is JUnit 4. Second, look for @RunWith on the class, which marks it as a JUnit 4 class. Third, check the build tool: if the same class passes from Maven and fails from the IDE, the IDE is using a different launcher, and the setting to change is "use the build tool to run tests" or the default JUnit version for the project. Fourth, list the engines on the test classpath with the dependency tree. Fifth, confirm the Surefire version, because a plugin old enough to predate the JUnit Platform ignores Jupiter classes entirely, which shows as zero tests run and no error at all.

A related message is java.lang.Exception: No tests found matching ... when filtering with -Dtest=. It is not the same problem: the class was found, but the pattern did not match any method. Check the spelling of the method and whether the class is nested, because nested classes need the Outer$Inner form.

Where this does not apply

If a class has valid org.junit.Test methods and still reports no runnable methods, check that they are public, non-static and annotated, and not inherited from a class that is package-private in another package. A base class with @RunWith forces the runner on every subclass. The Surefire JUnit Platform documentation covers the build side. The JUnit 4 to 5 migration tips in the user guide list the mappings.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.