dimanche 7 janvier 2018

Using Spring Boot TestRestTemplate relative path forces JSON conversion

According to the Spring Boot documentation:

Note that TestRestTemplate is now available as bean whenever @SpringBootTest is used. It’s pre-configured to resolve relative paths to http://localhost:${local.server.port}. We could have also used the @LocalServerPort annotation to inject the actual port that the server is running on into a test field.

I have a RESTFul application that returns XML data. The POM file contains jackson-dataformat-xml In the test class, I have the following code:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class XMLDataTest {

    @Autowired
    private TestRestTemplate restTemplate;
    ...
    ResponseEntity<List<City>> cities = restTemplate.exchange(appPath,
        HttpMethod.GET, null, paramType);

    assertThat(cities.getBody()).hasSize(8);
    assertThat(cities.getBody()).contains(this.c1, this.c2, this.c3);

If the appPath equals to the full URL, such as http://localhost:9234/myapp/cities, then the test runs OK. If appPath equals to a relative path, /myapp/cities, I get a JSON exception. It looks like relative paths force JSON conversion.

How to fix this?

Aucun commentaire:

Enregistrer un commentaire