I need to configure the underlying Jackson ObjectMapper in REST Assured. I am writing REST API tests using REST Assured and I need to define some filters to register with the ObjectMapper that is used to serialize my objects to JSON:
String newTestSuite = "{\"name\":\"Added through Rest API\",\"description\":\"Test Description\",\"steps\":[]}";
FilterProvider filters = new SimpleFilterProvider().addFilter("createNewTestSuite", new NewTestSuiteFilter());
ObjectMapper om = new ObjectMapper();
om.setFilters(filters);
try {
TestSuite ts = om.readValue(newCaspianTest, TestSuite.class);
RequestSpecification requestSpec = new RequestSpecBuilder()
.setBaseUri("https://somesite.com")
.setBody(ts)
.setUrlEncodingEnabled(false)
.build();
ResponseSpecification responseSpec = new ResponseSpecBuilder()
.expectStatusCode(200)
.expectStatusLine(Matchers.containsString("200 OK"))
.build();
RestAssured.given()
.auth().basic("testUser","testPassword")
.spec(requestSpec)
.log().all()
.post("/restendpoint")
.then()
.log().all()
.spec(responseSpec);
} catch(JsonParseException jpe) {
} catch(JsonMappingException jme) {
} catch(IOException ioe) {
}
}
}
You can try this:
RestAssured.config = RestAssuredConfig.config().objectMapperConfig(new ObjectMapperConfig().jackson2ObjectMapperFactory(
new Jackson2ObjectMapperFactory() {
@Override
public ObjectMapper create(Class aClass, String s) {
FilterProvider filter = new SimpleFilterProvider().addFilter(...);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setFilters(filter);
return objectMapper;
}
}
));