Search code examples
javajunitrunner

@RunWith not compiling in JUnit5


My project is set up with JUnit 5, and I want to use a different Runner for my tests, but I can't even use the @RunWith annotation because it won't compile. In this guide, https://www.baeldung.com/junit-5-runwith, it says that RunWith should still work in JUnit 5, so I'm confused why it won't even compile.
Even https://junit.org/junit5/docs/current/user-guide/#writing-tests-dependency-injection has an example of using RunWith if you scroll down a bit:
Using @RunWith(JUnitPlatform.class) will output all reported entries to stdout.
So why won't my code compile? Have I misunderstood something?

running gradlew build:

TestGameController.java:12: error: cannot find symbol
@RunWith(CacioTestRunner.class)
 ^
  symbol: class RunWith

Solution

  • JB Nizet answered my question in his comment.

    RunWith is a JUnit 4 annotation. See its package: org.junit.runner.RunWith. There is no "jupiter" in the package. The "equivalent" in JUnit 5 is ExtendWith, which expects a JUnit 5 extension class as argument. You'll need to add a dependency on JUnit 4, write the test with the JUnit 4 API, and use the vintage engine of JUnit 4 if there is no CacioExtension class.