DSG
DSG

Reputation: 1

JUnit test using JsonPath start failing after adding Twilio dependency

The question is not about how to convert the response to json, but why did the tests start failing?

The controller returns user information using a Java record object

@PostMapping("/login")
public ResponseEntity<Object> login(
   @RequestParam("email") String email, 
   @RequestParam("password") String password) {

    CurrentUser user = userService.findUser(email, password);
    return new ResponseEntity<>(user.toDTO(), HttpStatus.OK);
}


public record UserDTO(
    String id,
    String firstName,
    String lastName
){}

The tests were working fine using jsonpath to assert the response content

ResultActions response =
        mockMvc.perform(post("/users/login")
                .param("email", testEmail)
                .param("password", testPassword));

response
   .andExpect(status().isOk())
   .andExpect(jsonPath("$.firstName").value(testUser.getFirstName()));

Then, I just added the Twilio dependency

<dependency>
   <groupId>com.twilio.sdk</groupId>
   <artifactId>twilio</artifactId>
   <version>10.1.3</version>
</dependency>

and now I am getting the following error

Caused by: com.jayway.jsonpath.PathNotFoundException: Expected to find an object with property ['firstName'] in path $ but found 'java.lang.String'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.

I excluded the json-path from spring-boot-starter-test and added the com.fasterxml.jackson.core dependency but it didn't work, I got an error that the library is missing.

<exclusion>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
</exclusion>

If I just remove the Twilio dependency it works again.

I also know that I can just convert the response to JSON using any json converter and the tests will work even with Twilio dependency (already did it and it worked), but my biggest concern is, why did the tests start failing? should I exclude something from Twilio?

Upvotes: 0

Views: 52

Answers (1)

n1ck
n1ck

Reputation: 259

I encountered the same issue when trying to upgrade the Twilio dependency from an ancient version to the latest.

The issue is caused by the jackson-data-format-xml being present in your project through the Twilio SDK dependency. This causes all your MockMVC calls to return XML instead of JSON. Hence the tests will fail.

I fixed it by excluding the jackson-dataformat-xml from the Twilio dependency in the pom.xml:

<dependency>
    <groupId>com.twilio.sdk</groupId>
    <artifactId>twilio</artifactId>
    <version>${twilio.version}</version>
    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </exclusion>
    </exclusions>
</dependency

I do have to warn it might break some functionality in the library due to missing classes. In our case, we only send simple SMSes. This still works fine.

Upvotes: 0

Related Questions