我需要在REST Assured中配置底层的Jackson ObjectMapper。我正在使用REST Assured编写REST API测试,我需要定义一些过滤器来注册ObjectMapper,用于将我的对象序列化为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) {
}
}
}
答案 0 :(得分:15)
你可以试试这个:
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;
}
}
));
答案 1 :(得分:1)
这将为您提供一个对象映射器,当后端开发人员决定添加新字段时,该映射器不会爆炸。
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;
}
}
));
答案 2 :(得分:0)
@sanj的回答非常有帮助。我必须稍作修改,因为我正在使用RestAssuredMockMvc。
这是我将RestAssuredMockMvc配置为使用 snake_case 而不是 CamelCase 的方式:
RestAssuredMockMvc.config = RestAssuredMockMvcConfig.config().objectMapperConfig(new ObjectMapperConfig().jackson2ObjectMapperFactory(
(type, s) -> {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
return objectMapper;
}
));
答案 3 :(得分:0)
鉴于使用了“确保放心”的上下文,通常您经常需要修改或提供用于 some 请求的ObjectMapper
,但不是全部。
您可以使用以下方法来实现:
.body(Object object, ObjectMapper mapper)
或.content(Object object, ObjectMapper mapper)
(不推荐使用,建议使用.body()
)来序列化请求正文/有效负载。.as(Type cls, ObjectMapper mapper)
或.as(Class<T> cls, ObjectMapper mapper)
,以反序列化请求的响应正文。一个经典的用例,如@jason-henriksen所述
ReturnedType
类中定义的未标记为@JsonIgnore
的属性,而不会引发异常(在没有控件/无法修改该ReturnedType
类的情况下很有用 1 )这可以通过以下方式完成:
RequestSpecification request = ...;
io.restassured.mapper.ObjectMapper objectMapper = new Jackson2Mapper(((type, charset) -> {
com.fasterxml.jackson.databind.ObjectMapper om = new ObjectMapper().findAndRegisterModules();
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return om;
}));
ReturnType response = request.get().as(ReturnType.class, objectMapper);
1 您还可以通过使用Jackson MixIns配置ObjectMapper来实现此目的。