4

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
jeffrey.d.m
  • 616
  • 8
  • 23
  • 10
    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. – JB Nizet Nov 20 '19 at 23:03
  • Does this answer your question? [Equivalent for @RunWith(JUnitPlatform.class) for JUnit5](https://stackoverflow.com/questions/57462395/equivalent-for-runwithjunitplatform-class-for-junit5) – rogerdpack Feb 11 '22 at 20:01

2 Answers2

4

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.

David Buck
  • 3,752
  • 35
  • 31
  • 35
jeffrey.d.m
  • 616
  • 8
  • 23
1

I am also facing the same issue which gets resolved by adding the @ExtendWith(MockitoExtension.class) annotation before @RunWith annotation.