Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any special configuration to use SpringRunner with junit5?

My micro-service project based on spring-boot framework and all my unit test running with spring runner.

@RunWith(SpringRunner.class)

adding this annotations, imports the following library:

import org.springframework.test.context.junit4.SpringRunner;

How can I set my test classes to run with junit5 ?

like image 535
Ilan Miller Avatar asked Apr 07 '20 20:04

Ilan Miller


People also ask

Is JUnit 5 backwards compatible with JUnit 4?

Please note that JUnit 5 is not backward compatible with JUnit 4, but the JUnit team created the JUnit Vintage Project to support JUnit 4 test cases on top of JUnit 5.

Which type of JUnit4 rule is supported in JUnit5?

In JUnit 4, we used the @Rule and @ClassRule annotations to add special functionality to tests. In JUnit 5. we can reproduce the same logic using the @ExtendWith annotation.

What is the difference between SpringJUnit4ClassRunner and SpringRunner?

There is no difference, from the javadoc: SpringRunner is an alias for the SpringJUnit4ClassRunner.

How do I switch from JUnit4 to JUnit5?

Automatically Migrate All Tests Use Find Action with ⌘⇧A, or Ctrl+Shift+A, and type Migrate, to see migration options for the code. IntelliJ IDEA offers the option to migrate the code from JUnit4 to JUnit5. This migration is similar to what we did with the individual test class, but for all test classes.


Video Answer


3 Answers

Remove JUnit4 from your build Path.

For example :

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@TestPropertySource(locations = "classpath:application-local.properties")
public class MyTest {
    @Before
    public void setUp() {
        ...
    }

    @Test
    public void testMethod() {
        Assert.assertTrue(...);
    }
}

will become

@SpringBootTest(classes = Application.class)
@TestPropertySource(locations = "classpath:application-local.properties")
public class MyTest {
    @BeforeEach
    public void setUp() {
        ...
    }
    @Test
    public void testMethod() {
       Assertions.assertTrue(...);
    }
}
like image 81
cdesmetz Avatar answered Oct 21 '22 09:10

cdesmetz


Using JUnit Jupiter (aka JUnit 5) no longer requires ˋ @RunWith(SpringRunner.class)ˋ since this is a JUnit 4 mechanism. With recent versions of Spring/Spring Boot JUnit 5 support comes out of the box eg through using ˋspring-boot-starter-testˋ.

I recommend to exclude dependencies on JUnit 4 in your Maven/Gradle file to make confusing JUnit 4 and 5 features less likely.

Here’s an article that shows the basics: https://howtodoinjava.com/spring-boot2/testing/junit5-with-spring-boot2/

like image 28
johanneslink Avatar answered Oct 21 '22 09:10

johanneslink


Spring 2.4 seems to include JUnit 5 and make it the default out of the box.

Besides updating @RunWith(SpringJUnit4ClassRunner.class) to @ExtendWith(SpringExtension.class) I had to add the following to build.gradle for the tests to actually run:

test {
    useJUnitPlatform {}
}

This last step may have been due to JUnit 4 being a dependency of one of my dependencies, but every other thing I read didn't suggest this was needed.

like image 24
tschumann Avatar answered Oct 21 '22 08:10

tschumann