1

I am working with Spring Framework 4.3.6.RELEASE

For testing purposes through RestTemplate how a consumer, if the HTTP DELETE method fails because the entity does not exist, I need return an error message in raw format or String. It because 'normally' DELETE does not require mandatorily neither Accept nor Content-Type. I don't want complicate the user setting the Accept either for XML or JSON

My @Test method fails with the following error

java.lang.AssertionError: 
Expected: is "The entity does not exist"
     but: was "\"The entity does not exist\""

Observe the \" part

In the server side, I have declared:

@Bean
public StringHttpMessageConverter stringHttpMessageConverter(){
        StringHttpMessageConverter converter = new StringHttpMessageConverter();
        converter.setDefaultCharset(StandardCharsets.UTF_8);
        converter.setSupportedMediaTypes(Arrays.asList(new MediaType(MediaType.TEXT_PLAIN, StandardCharsets.UTF_8)));
        return converter;
}

And is registered:

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(httpMessageConverterConfig.mappingJackson2HttpMessageConverter());
        converters.add(httpMessageConverterConfig.mappingJackson2XmlHttpMessageConverter());
        converters.add(httpMessageConverterConfig.stringHttpMessageConverter());
}

With a @ControllerAdvice, working together with a method annotated with @ExceptionHandler, a ResponseEntity<Object> object is created with the following approach in some place to establish the Content-Type

if(httpServletRequest.getMethod().equals(HttpMethod.DELETE.toString())){
    headers.setContentType(new MediaType(MediaType.TEXT_PLAIN, StandardCharsets.UTF_8));
}

When I print the error message in the server side it prints without \" part

The RestTemplate for testing purposes is created with:

RestTemplate restTemplate = new RestTemplate(new MockMvcClientHttpRequestFactory(mockMvc));
restTemplate.setMessageConverters(customConverters);
restTemplate.setErrorHandler(restTemplateResponseErrorHandler);

Therefore why the \" part arrives to RestTemplate? How it can be fix it.? What missing configuration is need it?

Alpha

Test Class declaration

@Transactional
@WebAppConfiguration
@RunWith(Parameterized.class)
@ActiveProfiles(resolver=TestActiveProfilesResolver.class)
@ContextConfiguration(classes={RootApplicationContext.class, ServletApplicationContext.class})
@TestExecutionListeners(listeners={LoggingTestExecutionListener.class}, mergeMode=MergeMode.MERGE_WITH_DEFAULTS)
public class PersonaDeleteOneRestControllerClientBadRequestTest {

Some variables in that class:

@Autowired
private WebApplicationContext webApplicationContext;

@Autowired
private RestTemplateClientSupport restTemplateClientSupport;

private MockMvc mockMvc;

private RestTemplate restTemplate;
private static RestTemplateResponseErrorHandler restTemplateResponseErrorHandler;

Some creations:

@BeforeClass
public static void setUp_(){
    restTemplateResponseErrorHandler = new RestTemplateResponseErrorHandler();
}

@Before
public void setUp(){
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    restTemplate = restTemplateClientSupport.deleteOneBadRequest(mockMvc);
    restTemplate.setErrorHandler(restTemplateResponseErrorHandler);
}

The RestTemplate part is:

@Component
public class RestTemplateClientSupport {

private static final Logger logger = LoggerFactory.getLogger(RestTemplateClientSupport.class.getSimpleName());

private final List<HttpMessageConverter<?>> customConverters;

@Autowired
public RestTemplateClientSupport(List<HttpMessageConverter<?>> customConverters){
    this.customConverters = customConverters;
}

private RestTemplate createRestTemplateWithListHttpMessageConverter(MockMvc mockMvc){
    RestTemplate restTemplate = new RestTemplate(new MockMvcClientHttpRequestFactory(mockMvc));

    restTemplate.getMessageConverters().removeIf(converter -> {
        if(converter.getClass().getSimpleName().equals(MappingJackson2XmlHttpMessageConverter.class.getSimpleName())){
            logger.info("Removing: {}" , converter.getClass().getSimpleName());
            return true;
        }
        else{
            return false;
        }
    });
    restTemplate.getMessageConverters().removeIf(converter -> {
        if(converter.getClass().getSimpleName().equals(MappingJackson2HttpMessageConverter.class.getSimpleName())){
            logger.info("Removing: {}" , converter.getClass().getSimpleName());
            return true;
        }
        else{
            return false;
        }
    });

    restTemplate.getMessageConverters().removeIf(converter -> {
        if(converter.getClass().getSimpleName().equals(StringHttpMessageConverter.class.getSimpleName())){
            logger.info("Removing: {}" , converter.getClass().getSimpleName());
            return true;
        }
        else{
            return false;
        }
    });

    restTemplate.getMessageConverters().addAll(customConverters);
    return restTemplate;

Note: I am using restTemplate.getMessageConverters().removeIf(Predicate<>.. because I need remove these 3 default converters and add my own one created.

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158

0 Answers0