Skip to content

@JsonTest - Add support for specifying @JsonView to JacksonTester.write(...) #8672

@ilya40umov

Description

@ilya40umov

I'm writing a JsonTest for my DTO object. The DTO has a couple JsonViews declared on it. Naturally, I would like to be able to test json serialization for each view in my JsonTest. Current API does not allow specifying a particular view for JacksonTester.write(...)

For now, I ended up with a following hack to be able to set view on per test method basis:

public void useView(Class<?> view) {
        ObjectMapper objectMapper = (ObjectMapper) Whitebox.getInternalState(jacksonTester,
                "objectMapper");
        objectMapper = objectMapper.setConfig(objectMapper.getSerializationConfig().withView(view));
        Whitebox.setInternalState(jacksonTester, "objectMapper", objectMapper);
}

I would prefer to avoid creating a different test class for each DTO&View combination(so that I could configure the mapper in test setUp).

I'm also aware that I could test the behavior of my JsonViews in MVC tests, but since there is a type of test specifically dedicated to serialization to/deserialization from JSON, it makes more sense to test JsonViews in it.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions