From 0b1f2e78749c903c205d4da8b43e3ab2bfa90070 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Wed, 19 Jun 2024 12:16:39 +0200 Subject: [PATCH] Revert "Migrate MVC assertions to Spring Framework 6.2 M2." This reverts commit 880601514c44e37618ec82d96a58607a31aa64c4. $ Conflicts: $ server/pom.xml --- .../restbucks/AbstractWebIntegrationTest.java | 15 +++++---- .../web/OrderResourceIntegrationTest.java | 8 ++--- .../web/PaymentProcessIntegrationTest.java | 31 +++++++++---------- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/server/src/test/java/org/springsource/restbucks/AbstractWebIntegrationTest.java b/server/src/test/java/org/springsource/restbucks/AbstractWebIntegrationTest.java index 3ab0376..8a56422 100644 --- a/server/src/test/java/org/springsource/restbucks/AbstractWebIntegrationTest.java +++ b/server/src/test/java/org/springsource/restbucks/AbstractWebIntegrationTest.java @@ -27,9 +27,8 @@ import org.springframework.hateoas.client.LinkDiscoverer; import org.springframework.hateoas.client.LinkDiscoverers; import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.test.web.servlet.MvcResult; -import org.springframework.test.web.servlet.assertj.MockMvcTester; -import org.springframework.test.web.servlet.assertj.MvcTestResult; +import org.springframework.test.web.servlet.assertj.AssertableMockMvc; +import org.springframework.test.web.servlet.assertj.AssertableMvcResult; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.util.Assert; @@ -46,12 +45,12 @@ public abstract class AbstractWebIntegrationTest { @Autowired WebApplicationContext context; @Autowired LinkDiscoverers links; - protected MockMvcTester mvc; + protected AssertableMockMvc mvc; @BeforeEach void setUp() { - this.mvc = MockMvcTester.create(MockMvcBuilders.webAppContextSetup(context).// + this.mvc = AssertableMockMvc.create(MockMvcBuilders.webAppContextSetup(context).// defaultRequest(MockMvcRequestBuilders.get("/").locale(Locale.US)).// build()); } @@ -63,11 +62,11 @@ void setUp() { * @param rel must not be {@literal null}. * @return will never be {@literal null}. */ - protected Condition linkWithRel(LinkRelation rel) { + protected Condition linkWithRel(LinkRelation rel) { Assert.notNull(rel, "LinkRelation must not be null!"); - return new Condition<>(it -> hasLink(it.getMvcResult(), rel), "Expected to find link with relation %s!", rel); + return new Condition<>(it -> hasLink(it, rel), "Expected to find link with relation %s!", rel); } @SuppressWarnings("null") @@ -77,7 +76,7 @@ protected LinkDiscoverer getDiscovererFor(MockHttpServletResponse response) { @SneakyThrows @SuppressWarnings("null") - private boolean hasLink(MvcResult result, LinkRelation rel) { + private boolean hasLink(AssertableMvcResult result, LinkRelation rel) { var response = result.getResponse(); var content = response.getContentAsString(); diff --git a/server/src/test/java/org/springsource/restbucks/order/web/OrderResourceIntegrationTest.java b/server/src/test/java/org/springsource/restbucks/order/web/OrderResourceIntegrationTest.java index bafb35c..ca2c4bd 100644 --- a/server/src/test/java/org/springsource/restbucks/order/web/OrderResourceIntegrationTest.java +++ b/server/src/test/java/org/springsource/restbucks/order/web/OrderResourceIntegrationTest.java @@ -33,10 +33,10 @@ class OrderResourceIntegrationTest extends AbstractWebIntegrationTest { @Test void exposesOrdersResourceViaRootResource() throws Exception { - var result = mvc.perform(get("/")); // + var result = assertThat(mvc.perform(get("/"))); // - assertThat(result).hasStatus(HttpStatus.OK); - assertThat(result).contentType().isCompatibleWith(MediaTypes.HAL_JSON); - assertThat(result).bodyJson().hasPath("$._links.restbucks:orders.href").isNotNull(); + result.hasStatus(HttpStatus.OK); + result.contentType().isCompatibleWith(MediaTypes.HAL_JSON); + result.body().jsonPath().extractingPath("$._links.restbucks:orders.href").isNotNull(); } } diff --git a/server/src/test/java/org/springsource/restbucks/payment/web/PaymentProcessIntegrationTest.java b/server/src/test/java/org/springsource/restbucks/payment/web/PaymentProcessIntegrationTest.java index 0aa3904..c11a125 100644 --- a/server/src/test/java/org/springsource/restbucks/payment/web/PaymentProcessIntegrationTest.java +++ b/server/src/test/java/org/springsource/restbucks/payment/web/PaymentProcessIntegrationTest.java @@ -131,7 +131,7 @@ private MockHttpServletResponse accessRootResource() throws Exception { .hasStatus(HttpStatus.OK) .has(linkWithRel(ORDERS_REL)); - return response.getMvcResult().getResponse(); + return response.getResponse(); } /** @@ -154,7 +154,7 @@ private MockHttpServletResponse createNewOrder(MockHttpServletResponse source) t var drinksTemplate = parse.read("$._templates.default.properties[0].options.link.href", String.class); var drinksOptionsUri = Link.of(drinksTemplate).expand().getHref(); var drinksOptionsResponse = mvc.perform(get(drinksOptionsUri)) - .getMvcResult().getResponse().getContentAsString(); + .getResponse().getContentAsString(); var drinkUri = JsonPath.parse(drinksOptionsResponse).read("$._embedded.drinks[0].value", String.class); // Select location @@ -172,8 +172,7 @@ private MockHttpServletResponse createNewOrder(MockHttpServletResponse source) t .hasStatus(HttpStatus.CREATED) .headers().containsHeader(HttpHeaders.LOCATION); - return mvc.perform(get(response.getMvcResult().getResponse().getHeader(HttpHeaders.LOCATION))) - .getMvcResult().getResponse(); + return mvc.perform(get(response.getResponse().getHeader(HttpHeaders.LOCATION))).getResponse(); } /** @@ -191,11 +190,11 @@ private MockHttpServletResponse discoverOrdersResource(MockHttpServletResponse s LOG.info("Root resource returned: " + content); LOG.info(String.format("Found orders link pointing to %s… Following…", ordersLink)); - var result = mvc.perform(get(ordersLink.expand().getHref())); + var perform = mvc.perform(get(ordersLink.expand().getHref())); - assertThat(result).hasStatus(HttpStatus.OK); + assertThat(perform).hasStatus(HttpStatus.OK); - var response = result.getMvcResult().getResponse(); + var response = perform.getResponse(); LOG.info("Found orders: " + response.getContentAsString()); return response; @@ -229,15 +228,15 @@ private MockHttpServletResponse accessFirstOrder(MockHttpServletResponse source) LOG.info(String.format("Picking first order using JSONPath expression %s…", FIRST_ORDER_EXPRESSION)); LOG.info(String.format("Discovered self link pointing to %s… Following", orderLink)); - var result = mvc.perform(get(orderLink.getHref())); + var perform = mvc.perform(get(orderLink.getHref())); - assertThat(result) + assertThat(perform) .has(linkWithRel(IanaLinkRelations.SELF)) .has(linkWithRel(CANCEL_REL)) .has(linkWithRel(UPDATE_REL)) .has(linkWithRel(PAYMENT_REL)); - return result.getMvcResult().getResponse(); + return perform.getResponse(); } /** @@ -280,7 +279,7 @@ private MockHttpServletResponse triggerPayment(MockHttpServletResponse response) assertThat(mvc.perform(delete(selfLink.getHref()))) .hasStatus(HttpStatus.METHOD_NOT_ALLOWED); - return action.getMvcResult().getResponse(); + return action.getResponse(); } /** @@ -314,7 +313,7 @@ private MockHttpServletResponse pollUntilOrderHasReceiptLink(MockHttpServletResp LOG.info("Poll state of order until receipt is ready…"); var action = mvc.perform(get(orderLink.expand().getHref()).headers(headers)); - pollResponse = action.getMvcResult().getResponse(); + pollResponse = action.getResponse(); var status = pollResponse.getStatus(); etag = pollResponse.getHeader("ETag"); @@ -359,7 +358,7 @@ private MockHttpServletResponse takeReceipt(MockHttpServletResponse response) th assertThat(result).hasStatus(HttpStatus.OK); - var receiptResponse = result.getMvcResult().getResponse(); + var receiptResponse = result.getResponse(); LOG.info("Accessing receipt, got:" + receiptResponse.getContentAsString()); LOG.info("Taking receipt…"); @@ -368,7 +367,7 @@ private MockHttpServletResponse takeReceipt(MockHttpServletResponse response) th assertThat(result).hasStatus(HttpStatus.OK); - return result.getMvcResult().getResponse(); + return result.getResponse(); } /** @@ -389,9 +388,9 @@ private void verifyOrderTaken(MockHttpServletResponse response) throws Exception .doesNotHave(linkWithRel(UPDATE_REL)) .doesNotHave(linkWithRel(CANCEL_REL)) .doesNotHave(linkWithRel(PAYMENT_REL)) - .bodyJson().hasPathSatisfying("$.status", it -> assertThat(it).isEqualTo("Delivered")); + .body().jsonPath().extractingPath("$.status").isEqualTo("Delivered"); - LOG.info("Final order state: " + result.getMvcResult().getResponse().getContentAsString()); + LOG.info("Final order state: " + result.getResponse().getContentAsString()); } /**