Skip to content

Commit

Permalink
Migrate MVC assertions to Spring Framework 6.2 M2.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed May 17, 2024
1 parent c2f2d35 commit c84094b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
<java.version>22</java.version>
<lombok.version>1.18.32</lombok.version>
<jmolecules.version>2023.1.3</jmolecules.version>
<spring-framework.version>6.2.0-M1</spring-framework.version>
<spring-framework.version>6.2.0-M2</spring-framework.version>
<spring-modulith.version>1.2.0-RC1</spring-modulith.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
import org.springframework.hateoas.client.LinkDiscoverer;
import org.springframework.hateoas.client.LinkDiscoverers;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.assertj.AssertableMockMvc;
import org.springframework.test.web.servlet.assertj.AssertableMvcResult;
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.Assert;
Expand All @@ -45,12 +46,12 @@ public abstract class AbstractWebIntegrationTest {
@Autowired WebApplicationContext context;
@Autowired LinkDiscoverers links;

protected AssertableMockMvc mvc;
protected MockMvcTester mvc;

@BeforeEach
void setUp() {

this.mvc = AssertableMockMvc.create(MockMvcBuilders.webAppContextSetup(context).//
this.mvc = MockMvcTester.create(MockMvcBuilders.webAppContextSetup(context).//
defaultRequest(MockMvcRequestBuilders.get("/").locale(Locale.US)).//
build());
}
Expand All @@ -62,11 +63,11 @@ void setUp() {
* @param rel must not be {@literal null}.
* @return will never be {@literal null}.
*/
protected Condition<AssertableMvcResult> linkWithRel(LinkRelation rel) {
protected Condition<MvcTestResult> linkWithRel(LinkRelation rel) {

Assert.notNull(rel, "LinkRelation must not be null!");

return new Condition<>(it -> hasLink(it, rel), "Expected to find link with relation %s!", rel);
return new Condition<>(it -> hasLink(it.getMvcResult(), rel), "Expected to find link with relation %s!", rel);
}

@SuppressWarnings("null")
Expand All @@ -76,7 +77,7 @@ protected LinkDiscoverer getDiscovererFor(MockHttpServletResponse response) {

@SneakyThrows
@SuppressWarnings("null")
private boolean hasLink(AssertableMvcResult result, LinkRelation rel) {
private boolean hasLink(MvcResult result, LinkRelation rel) {

var response = result.getResponse();
var content = response.getContentAsString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class OrderResourceIntegrationTest extends AbstractWebIntegrationTest {
@Test
void exposesOrdersResourceViaRootResource() throws Exception {

var result = assertThat(mvc.perform(get("/"))); //
var result = mvc.perform(get("/")); //

result.hasStatus(HttpStatus.OK);
result.contentType().isCompatibleWith(MediaTypes.HAL_JSON);
result.body().jsonPath().extractingPath("$._links.restbucks:orders.href").isNotNull();
assertThat(result).hasStatus(HttpStatus.OK);
assertThat(result).contentType().isCompatibleWith(MediaTypes.HAL_JSON);
assertThat(result).bodyJson().hasPath("$._links.restbucks:orders.href").isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private MockHttpServletResponse accessRootResource() throws Exception {
.hasStatus(HttpStatus.OK)
.has(linkWithRel(ORDERS_REL));

return response.getResponse();
return response.getMvcResult().getResponse();
}

/**
Expand All @@ -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))
.getResponse().getContentAsString();
.getMvcResult().getResponse().getContentAsString();
var drinkUri = JsonPath.parse(drinksOptionsResponse).read("$._embedded.drinks[0].value", String.class);

// Select location
Expand All @@ -172,7 +172,8 @@ private MockHttpServletResponse createNewOrder(MockHttpServletResponse source) t
.hasStatus(HttpStatus.CREATED)
.headers().containsHeader(HttpHeaders.LOCATION);

return mvc.perform(get(response.getResponse().getHeader(HttpHeaders.LOCATION))).getResponse();
return mvc.perform(get(response.getMvcResult().getResponse().getHeader(HttpHeaders.LOCATION)))
.getMvcResult().getResponse();
}

/**
Expand All @@ -194,7 +195,7 @@ private MockHttpServletResponse discoverOrdersResource(MockHttpServletResponse s

assertThat(perform).hasStatus(HttpStatus.OK);

var response = perform.getResponse();
var response = perform.getMvcResult().getResponse();

LOG.info("Found orders: " + response.getContentAsString());
return response;
Expand Down Expand Up @@ -236,7 +237,7 @@ private MockHttpServletResponse accessFirstOrder(MockHttpServletResponse source)
.has(linkWithRel(UPDATE_REL))
.has(linkWithRel(PAYMENT_REL));

return perform.getResponse();
return perform.getMvcResult().getResponse();
}

/**
Expand Down Expand Up @@ -279,7 +280,7 @@ private MockHttpServletResponse triggerPayment(MockHttpServletResponse response)
assertThat(mvc.perform(delete(selfLink.getHref())))
.hasStatus(HttpStatus.METHOD_NOT_ALLOWED);

return action.getResponse();
return action.getMvcResult().getResponse();
}

/**
Expand Down Expand Up @@ -313,7 +314,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.getResponse();
pollResponse = action.getMvcResult().getResponse();

var status = pollResponse.getStatus();
etag = pollResponse.getHeader("ETag");
Expand Down Expand Up @@ -358,7 +359,7 @@ private MockHttpServletResponse takeReceipt(MockHttpServletResponse response) th

assertThat(result).hasStatus(HttpStatus.OK);

var receiptResponse = result.getResponse();
var receiptResponse = result.getMvcResult().getResponse();

LOG.info("Accessing receipt, got:" + receiptResponse.getContentAsString());
LOG.info("Taking receipt…");
Expand All @@ -367,7 +368,7 @@ private MockHttpServletResponse takeReceipt(MockHttpServletResponse response) th

assertThat(result).hasStatus(HttpStatus.OK);

return result.getResponse();
return result.getMvcResult().getResponse();
}

/**
Expand All @@ -388,9 +389,9 @@ private void verifyOrderTaken(MockHttpServletResponse response) throws Exception
.doesNotHave(linkWithRel(UPDATE_REL))
.doesNotHave(linkWithRel(CANCEL_REL))
.doesNotHave(linkWithRel(PAYMENT_REL))
.body().jsonPath().extractingPath("$.status").isEqualTo("Delivered");
.bodyJson().hasPathSatisfying("$.status", it -> assertThat(it).isEqualTo("Delivered"));

LOG.info("Final order state: " + result.getResponse().getContentAsString());
LOG.info("Final order state: " + result.getMvcResult().getResponse().getContentAsString());
}

/**
Expand Down

0 comments on commit c84094b

Please sign in to comment.