From 1c2201faa1773cbc843d0a1383ea29d8b0f687d6 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 5 Jan 2015 12:17:35 +0100 Subject: [PATCH] DATAREST-427 - Fixed NullPointerException in RepositoryRestExceptionHandler. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Handling HttpMessageNotReadableException in RepositoryRestExceptionHandler previously handed null to the badRequest(…) method resulting in an assertion failure in response(…) eventually. This is now fixed by handing in an HttpHeaders instance. --- .../RepositoryRestExceptionHandler.java | 2 +- ...positoryRestExceptionHandlerUnitTests.java | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestExceptionHandlerUnitTests.java diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestExceptionHandler.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestExceptionHandler.java index 1d7ce7206..c37e7092c 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestExceptionHandler.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestExceptionHandler.java @@ -159,7 +159,7 @@ public class RepositoryRestExceptionHandler { } private ResponseEntity badRequest(T throwable) { - return badRequest(null, throwable); + return badRequest(new HttpHeaders(), throwable); } private ResponseEntity badRequest(HttpHeaders headers, T throwable) { diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestExceptionHandlerUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestExceptionHandlerUnitTests.java new file mode 100644 index 000000000..b21f2a237 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestExceptionHandlerUnitTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.rest.webmvc; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; +import org.springframework.context.support.StaticMessageSource; +import org.springframework.data.rest.webmvc.support.ExceptionMessage; +import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; + +/** + * Unit tests for {@link RepositoryRestExceptionHandler}. + * + * @author Oliver Gierke + */ +public class RepositoryRestExceptionHandlerUnitTests { + + static final RepositoryRestExceptionHandler HANDLER = new RepositoryRestExceptionHandler(new StaticMessageSource()); + + /** + * @see DATAREST-427 + */ + @Test + public void handlesHttpMessageNotReadableException() { + + ResponseEntity result = HANDLER + .handleNotReadable(new HttpMessageNotReadableException("Message!")); + + assertThat(result.getStatusCode(), is(org.springframework.http.HttpStatus.BAD_REQUEST)); + } +}