0

I have a very similar problem to this post: Rest Assured doesn't use custom ObjectMapper

However, I am using slightly different configurations/models and the provided answer did not solve my problem.

When running the mock mvc test, my custom ObjectMapper config is not getting picked up. I have tried attaching this config class to the RestAssuredMockMvc, but it still seems to not pick this up when the standalone setup fires.

Any ideas?

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public abstract class BaseClassForContractTests {

@Autowired
Controller controller;

@Before
public void setup() {

    RestAssuredMockMvc.config = RestAssuredMockMvcConfig.config().objectMapperConfig(
            ObjectMapperConfig.objectMapperConfig().jackson2ObjectMapperFactory(new Jackson2ObjectMapperFactory() {
                @SuppressWarnings("rawtypes")
                @Override
                public ObjectMapper create(Class cls, String charset) {
                    ObjectMapper objectMapper = new ObjectMapper();
                    objectMapper.registerModule(new JavaTimeModule());
                    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
                    //objectMapper.setTimeZone(TimeZone.getTimeZone("America/New_York"));
                    //objectMapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true);
                    return objectMapper;
                }
            })
    );

    RestAssuredMockMvc.standaloneSetup(controller);

}

}

Keifer
  • 165
  • 1
  • 2
  • 11

2 Answers2

1

I have setup object mapper with RestAssuredMockMvc.standaloneSetup and it worked for me.

MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new
MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setObjectMapper(new ObjectMapper()));
RestAssuredMockMvc.standaloneSetup(
     MockMvcBuilders.standaloneSetup(yourcontroller)
             .setMessageConverters(mappingJackson2HttpMessageConverter)
);

The full solution is described in this article

0

Yep, it was something stupid/simple. If anyone has this issue with no configs getting picked up, you should be using RestAssuredMockMvc.webAppContextSetup(context); instead of standaloneSetup. All you have to do is @Autowire a webAppContext.

Keifer
  • 165
  • 1
  • 2
  • 11