Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the underlying Jackson ObjectMapper in REST Assured?

Tags:

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) {

    }
}

}

like image 646
Selena Avatar asked Jul 31 '14 22:07

Selena


People also ask

How do I use ObjectMapper in Restassured?

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.

How do I read JSON file with ObjectMapper?

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);

What is ObjectMapper class in Jackson?

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.

Should I declare Jackson's ObjectMapper as a static field?

Yes, that is safe and recommended.


2 Answers

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;
        }
    }
));
like image 153
sanj Avatar answered Oct 02 '22 16:10

sanj


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;
      }          

    }
));
like image 31
Jason Henriksen Avatar answered Oct 02 '22 15:10

Jason Henriksen