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) {
}
}
}
Interface ObjectMapperAn object mapper is used to serialize and deserialize a Java object to and from a String, byte[] or InputStream. REST Assured provides mappers for XML and JSON out of the box (see ObjectMapperType ) but you can implement this interface to roll your own mapper implementations for custom formats.
Read Object From JSON via URL ObjectMapper objectMapper = new ObjectMapper(); URL url = new URL("file:data/car. json"); Car car = objectMapper. readValue(url, Car. class);
ObjectMapper is the main actor class of Jackson library. ObjectMapper class ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (JsonNode), as well as related functionality for performing conversions.
Yes, that is safe and recommended.
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;
}
}
));
This will give you an object mapper that does not explode when the back end developer decides to add a new field.
RestAssured.config = RestAssuredConfig.config().objectMapperConfig(new ObjectMapperConfig().jackson2ObjectMapperFactory(
new Jackson2ObjectMapperFactory() {
@Override
public ObjectMapper create(Type cls, String charset) {
ObjectMapper om = new ObjectMapper().findAndRegisterModules();
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return om;
}
}
));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With