diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/PersistentEntityResourceHandlerMethodArgumentResolver.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/PersistentEntityResourceHandlerMethodArgumentResolver.java index b02e12088..a953392cc 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/PersistentEntityResourceHandlerMethodArgumentResolver.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/PersistentEntityResourceHandlerMethodArgumentResolver.java @@ -155,7 +155,7 @@ public class PersistentEntityResourceHandlerMethodArgumentResolver implements Ha if (request.isPatchRequest() && converter instanceof MappingJackson2HttpMessageConverter) { if (objectToUpdate == null) { - new ResourceNotFoundException(); + throw new ResourceNotFoundException(); } ObjectMapper mapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper(); diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java index a051f7659..58c08cd2b 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java @@ -24,6 +24,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import net.minidev.json.JSONArray; +import java.net.URI; import java.util.List; import java.util.Map; @@ -31,6 +32,7 @@ import org.junit.Test; import org.springframework.hateoas.Link; import org.springframework.hateoas.Links; import org.springframework.hateoas.MediaTypes; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.web.servlet.ResultActions; @@ -242,4 +244,27 @@ public abstract class CommonWebTests extends AbstractWebIntegrationTests { assertThat(links.hasLink("profile"), is(true)); } } + + /** + * @see DATAREST-661 + */ + @Test + public void patchToNonExistingResourceReturnsNotFound() throws Exception { + + String rel = expectedRootLinkRels().iterator().next(); + String uri = client.discoverUnique(rel).expand().getHref().concat("/"); + String id = "4711"; + Integer status = null; + + do { + + // Try to find non existing resource + uri = uri.concat(id); + status = mvc.perform(get(URI.create(uri))).andReturn().getResponse().getStatus(); + + } while (status != HttpStatus.NOT_FOUND.value()); + + // PATCH to non-existing resource + mvc.perform(patch(URI.create(uri))).andExpect(status().isNotFound()); + } }