Skip to content

[UnirestMock] thenReturn(Object pojo) assumes JSON serialization #394

@Tazaf

Description

@Tazaf

Is your feature request related to a problem? Please describe.
I'm working against a REST Api that (sadly) only returns XML formatted responses and I wanted to use UnirestMock in my tests, notably the thenReturn(Object pojo) method.

Alas, that can not work as, like the title says, the thenReturn(Object pojo) method serializes the provided Object pojo to a JSON string as the response's body:

this.response = new JsonObjectMapper().writeValue(pojo);

The Unirest instance I'm mocking in my tests has been configured to use Jackson XmlMapper, thus when processing the MockRawResponse to produce an ObjectResponse:

.map(s -> getBody(s, e -> om.readValue(e, to)))

It fails due to a parsing exception:

"com.fasterxml.jackson.core.JsonParseException: Unexpected character '{' (code 123) in prolog; expected '<' at [row,col {unknown-source}]: [1,1]"

And of course it does: my ObjectMapper expects some XML and is provided JSON instead

As a consequence, the body of the response is null and my test fails.

Describe the solution you'd like
I'd like to be able to somehow provide a custom ObjectMapper to a MockClient instance, that will be used when serialiazing POJOs.

Something like (or anything else that makes more sense):

this.instanceMock = MockClient.register(this.customInstance, new CustomObjectMapper());

Describe alternatives you've considered
Of course, I could simply the XML I want to see returned as a String argument to the `thenReturn(String body) but... I'd very much like not to do that 😅
(I'll do that in the meantime, though, but still)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions