Search code examples
spring-bootjunitjunit5spring-boot-testspringrunner

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 ?


Solution

  • 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(...);
        }
    }