Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit5 with spring-boot 1.5

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.

like image 792
tgr Avatar asked Dec 29 '17 08:12

tgr


People also ask

Does JUnit 5 work with Java 7?

JUnit 5 tests will run with Java 8 or above.

What version of JUnit does spring boot starter test use?

By default, spring-boot-starter-test dependency imports the junit 4 dependencies into Spring boot application.

Is JUnit 5 faster than junit4?

JUnit 5 is 10x slower than JUnit 4 #880.

Does spring boot include JUnit?

Anatomy of the Spring Boot Starter Test This starter includes Spring-specific dependencies and dependencies for auto-configuration and a set of testing libraries. This includes JUnit, Mockito, Hamcrest, AssertJ, JSONassert, and JsonPath.


1 Answers

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.

like image 191
Arho Huttunen Avatar answered Sep 18 '22 17:09

Arho Huttunen