4

I'm currently using Spring Boot 1.5.4 alongside Junit 5 and Java 8.

I want to setup integration tests on multiple entries that are stored in a csv file using Junit's ParameterizedTest.

Here is the code:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class MatchingIT {

    @Autowired
    private TestRestTemplate template;

    @ParameterizedTest
    @MethodSource(names = "vehicles")
    void nonRegressionTests(EDIVehicle vehicle) {
        ResponseEntity<Vehicle> v = template.getForEntity("/match/" + vehicule.getId(), Vehicle.class);
        Assert.assertNotNull(v);
    }

    private static Stream<EDIVehicle> vehicles() throws IOException {
        InputStream is = new ClassPathResource("/entries.csv").getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        return br.lines().map(toVehicle);
    }

    private static Function<String, EDIVehicle> toVehicle = (line) -> {
        String[] p = line.split("\\|");
        return new EDIVehicle(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12]);
    };
}

I know from the documentation that:

If you are using the @SpringBootTest annotation, a TestRestTemplate is automatically available and can be @Autowired into your test.

The thing is that I do use the SpringBootTest annotation but when I run the tests, the TestRestTemplate is constantly null. Maybe I missed something.

EDIT: I do have the exact same problem while adding @RunWith(SpringRunner.class) annotation

Rlarroque
  • 1,900
  • 1
  • 17
  • 27
  • 1
    Are you using JUnit 5? I don't think there is first class support from Spring Boot for JUnit 5 yet. There is an extension that may help you: https://github.com/sbrannen/spring-test-junit5 – LucasP Jun 13 '17 at 16:09
  • Can you check from which package you imported `TestRestTemplate`? `org.springframework.boot.test.web.client` or `org.springframework.boot.test` ? – harshavmb Jun 13 '17 at 16:26
  • @harshavmb I'm using org.springframework.boot.test.web.client and not the deprecated one. – Rlarroque Jun 14 '17 at 07:41
  • @LucasP I'l using JUnit 5 yes. I'll try with JUnit 4 to make sure the problem doesn't come from the JUnit version. – Rlarroque Jun 14 '17 at 07:42
  • @Lucas I fixed my issue by using the dependency you mentioned. Thx! The thing is, my tests are not ran by maven when doing a 'mvn clean install' – Rlarroque Jun 14 '17 at 09:28
  • EDIT: It works fine by adding the '@RunWith(JUnitPlatform.class)' annotation with the right dependency. – Rlarroque Jun 14 '17 at 09:35

3 Answers3

3

I ended up using the dependency from the following repo: github.com/sbrannen/spring-test-junit5 as mentioned @LucasP

Since there is no first class support for JUnit 5 in Spring 4.3 and below it helped me autowiring correctly the Spring TestRestTemplate by using @ExtendWith(SpringExtension.class) on my test class.

Next step would be to directly use Spring Boot 2.0 for a better support of JUnit 5.

Rlarroque
  • 1,900
  • 1
  • 17
  • 27
1

@SpringBootTest is not compleatelly compatible with JUnit 5. However you can workaround this by using the next test declaration:

@RunWith(JUnitPlatform.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
class MatchingIT {...}

Note: You have to upgrade your spring-boot-starter-test version to 2.0.0.M1 to be able to use org.springframework.test.context.junit.jupiter.SpringExtension (the simplest way would be using start.spring.io for generating 2.0.0.M1-dependent project and taking needed maven/gradle dependencies from there).

Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148
  • I'm not sure that I already want to use Spring 5 in my project. I might try if I can't find any other solution and I'll come back to you. – Rlarroque Jun 14 '17 at 07:44
  • You don't need to use Spring 5 for whole project. You need it only in tests. You're already using Junit 5 which is not released yet - adding one more unreleased test dependency should not be a problem. – Sasha Shpota Jun 14 '17 at 07:48
0

I think you are missing the @RunWith(SpringRunner.class) and also make sure your class which initialize the context (usually the one that contains the main method) have a @SpringBootApplication annotation (else @SpringBootTest doesn't find the context to load for the test.

  • As I said to David Florez I do have the exact same problem even with the @RunWith(SpringRunner.class) annotation. I'm looking into contexts. – Rlarroque Jun 13 '17 at 16:05
  • I think @RunWith(SpringRunner.class) would be one of possible solution to your Problem. The difference between SpringRunner and SpringBootTest is specified in https://stackoverflow.com/questions/58901288/springrunner-vs-springboottest – Ben Cheng Aug 17 '20 at 05:49