17

I have a spring-boot application which uses spring-boot version 1.5.9.RELEASE. To test this application I want to use junit-jupiter version 5.0.2.

For simple service tests it works without any problems. But when it comes to testing rest endpoints, I am failing. The reason is the @RunWith(SpringRunner.class) annotation, which I used with junit4 to wire the everything together.

Is there a SpringRunner for junit5 before spring-boot 2?


Update

I just stumbled over an article on how to run JUnit5 tests. Point 4 looks like a promising start.

tgr
  • 3,557
  • 4
  • 33
  • 63

1 Answers1

25

To migrate from JUnit 4 to JUnit 5 you can replace @RunWith(SpringRunner.class) with @ExtendWith(SpringExtension.class).

Unfortunately, spring-boot version 1.5.9-RELEASE is based on Spring 4 and the SpringExtension is only available since Spring 5.

However, there is a solution since it's possible to use JUnit 5 and SpringExtension in Spring 4 by using spring-test-junit5. Just check the instructions for setting up the dependencies.

Arho Huttunen
  • 1,081
  • 9
  • 16
  • 1
    If doing a full conversion to JUnit5, remove JUnit4 with: testCompile('org.springframework.boot:spring-boot-starter-test') { exclude group: 'junit', module: 'junit' } – Arlo Mar 05 '19 at 18:50
  • Could you please share the instructions instead of posting a link? Links may easily break, especially if you're linking a github readme – Christian Vincenzo Traina Jun 03 '22 at 07:42