Is there any special configuration to use SpringRunner with junit5? [duplicate]
Asked Answered
E

3

19

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 ?

Escarpment answered 7/4, 2020 at 20:16 Comment(0)
P
22

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(...);
    }
}
Paresthesia answered 17/9, 2020 at 15:44 Comment(1)
I got one issue on same while I'm using @Test annotation of com.junit.test(4) it's working fine but while using same thing com.jupiter.junit.test then it gives illegalstate error in contract testingAdrenocorticotropic
A
8

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.

Alembic answered 12/4, 2021 at 1:43 Comment(0)
A
0

The first annotation @RunWith(SpringRunner.class) is used to provide a bridge between Spring Boot test features and JUnit. SpringRunner.class enables full support of spring context loading and dependency injection of the beans in the tests. @SpringBootTest create ApplicationContext tests through SpringApplication that will be utilized in our tests. It bootstraps the entire container since the embedded server and creates a web environment.

In our test, we can mimic the real web environment setting it as RANDOM_PORT that also loads WebServerApplicationContext. The embedded server is started and listen to on a random port.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {YourPackage.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class YourClassTest {

    @LocalServerPort
    private int port;

    @Autowired
    TestRestTemplate restTemplate;
    HttpHeaders headers = new HttpHeaders();

    @ParameterizedTest
    @JsonFileSource(resources = "/param.json")
    void createBusinessEntity(JsonObject object){
      ....
    }
}

@LocalServerPort annotation provides us the injected HTTP port that got allocated at runtime. It is a convenient alternative for @Value("${local.server.port}").

To access a third-party REST service inside a Spring application we use the Spring RestTemplate or TestRestTemplate the convenient alternative that is suitable for integration tests by injecting it in our test class. With spring-boot-starter-test dependency in our project, we can access to "TestRestTemplate" class in runtime.

In our test method, we are using the junit-json-params , a Junit 5 library that provides annotations to load data from JSON Strings or files in parameterized tests. We also annotated the method with @ParameterizedTest annotation to complement the library bellow. It is used to signal the annotated method is a parameterized test method. That method must not be private or static. They also must specify at least one ArgumentsProvider via @ArgumentsSource or a corresponding composed annotation.

Our @ArgumentsSource a JSON file @JsonFileSource(resources = "param.json") we put inside the test.resources package. @JsonFileSource lets you use JSON files from the classpath. It supports single objects, arrays of objects and JSON primitives.

The JSON object retrieved from the file is bound to the method params "object" that it is converted to a POJO object, in this case, our entity model.

In the Pom.xml we must import these libraries...

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>

        <exclusions>
            <exclusion>
                <groupId>com.vaadin.external.google</groupId>
                <artifactId>android-json</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

       <dependency>
            <groupId>net.joshka</groupId>
            <artifactId>junit-json-params</artifactId>
            <version>5.5.1-r0</version>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>${mockito.version}</version>
        </dependency>

        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>

Take a look at these articles that a post on DZone and my blog where you can access a complete sample and explanation step by step how to test spring boot microservice using Junit 5. https://dzone.com/articles/microservices-in-publish-subscribe-communication-u https://www.jeevora.com/2019/11/18/publish-subscribe-messaging-systems/

Appeal answered 8/4, 2020 at 1:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.