Skip to content

Commit 6e547db

Browse files
committed
Migrate to Spring 6.2's AssertJ based MVC assertions.
1 parent 48af787 commit 6e547db

File tree

3 files changed

+106
-114
lines changed

3 files changed

+106
-114
lines changed
Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2019 the original author or authors.
2+
* Copyright 2013-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,26 +15,23 @@
1515
*/
1616
package org.springsource.restbucks;
1717

18-
import static org.assertj.core.api.Assertions.*;
19-
20-
import lombok.RequiredArgsConstructor;
18+
import lombok.SneakyThrows;
2119

2220
import java.util.Locale;
23-
import java.util.Optional;
2421

22+
import org.assertj.core.api.Condition;
2523
import org.junit.jupiter.api.BeforeEach;
2624
import org.springframework.beans.factory.annotation.Autowired;
2725
import org.springframework.boot.test.context.SpringBootTest;
28-
import org.springframework.hateoas.Link;
2926
import org.springframework.hateoas.LinkRelation;
3027
import org.springframework.hateoas.client.LinkDiscoverer;
3128
import org.springframework.hateoas.client.LinkDiscoverers;
3229
import org.springframework.mock.web.MockHttpServletResponse;
33-
import org.springframework.test.web.servlet.MockMvc;
34-
import org.springframework.test.web.servlet.MvcResult;
35-
import org.springframework.test.web.servlet.ResultMatcher;
30+
import org.springframework.test.web.servlet.assertj.AssertableMockMvc;
31+
import org.springframework.test.web.servlet.assertj.AssertableMvcResult;
3632
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
3733
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
34+
import org.springframework.util.Assert;
3835
import org.springframework.web.context.WebApplicationContext;
3936

4037
/**
@@ -48,66 +45,43 @@ public abstract class AbstractWebIntegrationTest {
4845
@Autowired WebApplicationContext context;
4946
@Autowired LinkDiscoverers links;
5047

51-
protected MockMvc mvc;
48+
protected AssertableMockMvc mvc;
5249

5350
@BeforeEach
5451
void setUp() {
5552

56-
mvc = MockMvcBuilders.webAppContextSetup(context).//
53+
this.mvc = AssertableMockMvc.create(MockMvcBuilders.webAppContextSetup(context).//
5754
defaultRequest(MockMvcRequestBuilders.get("/").locale(Locale.US)).//
58-
build();
55+
build());
5956
}
6057

6158
/**
62-
* Creates a {@link ResultMatcher} that checks for the presence of a link with the given rel.
59+
* Creates a AssertJ {@link Condition} that checks for the presence of a {@link Link} with the given
60+
* {@link LinkRelation}.
6361
*
64-
* @param rel
65-
* @return
62+
* @param rel must not be {@literal null}.
63+
* @return will never be {@literal null}.
6664
*/
67-
protected ResultMatcher linkWithRelIsPresent(LinkRelation rel) {
68-
return new LinkWithRelMatcher(rel, true);
69-
}
65+
protected Condition<AssertableMvcResult> linkWithRel(LinkRelation rel) {
7066

71-
/**
72-
* Creates a {@link ResultMatcher} that checks for the non-presence of a link with the given rel.
73-
*
74-
* @param rel
75-
* @return
76-
*/
77-
protected ResultMatcher linkWithRelIsNotPresent(LinkRelation rel) {
78-
return new LinkWithRelMatcher(rel, false);
67+
Assert.notNull(rel, "LinkRelation must not be null!");
68+
69+
return new Condition<>(it -> hasLink(it, rel), "Expected to find link with relation %s!", rel);
7970
}
8071

72+
@SuppressWarnings("null")
8173
protected LinkDiscoverer getDiscovererFor(MockHttpServletResponse response) {
8274
return links.getRequiredLinkDiscovererFor(response.getContentType());
8375
}
8476

85-
@RequiredArgsConstructor
86-
private class LinkWithRelMatcher implements ResultMatcher {
87-
88-
private final LinkRelation rel;
89-
private final boolean present;
90-
91-
/*
92-
* (non-Javadoc)
93-
* @see org.springframework.test.web.servlet.ResultMatcher#match(org.springframework.test.web.servlet.MvcResult)
94-
*/
95-
@Override
96-
public void match(MvcResult result) throws Exception {
97-
98-
MockHttpServletResponse response = result.getResponse();
99-
String content = response.getContentAsString();
100-
LinkDiscoverer discoverer = links.getRequiredLinkDiscovererFor(response.getContentType());
101-
102-
Optional<Link> link = discoverer.findLinkWithRel(rel, content);
103-
104-
assertThat(link).matches(it -> it.isPresent() == present, getMessage(link));
105-
}
77+
@SneakyThrows
78+
@SuppressWarnings("null")
79+
private boolean hasLink(AssertableMvcResult result, LinkRelation rel) {
10680

107-
private String getMessage(Optional<Link> link) {
81+
var response = result.getResponse();
82+
var content = response.getContentAsString();
83+
var discoverer = links.getRequiredLinkDiscovererFor(response.getContentType());
10884

109-
return String.format("Expected to %s link with relation %s, but found %s!",
110-
present ? "find" : "not find", rel, present ? link.get() : "none");
111-
}
85+
return discoverer.findLinkWithRel(rel, content).isPresent();
11286
}
11387
}

server/src/test/java/org/springsource/restbucks/order/web/OrderResourceIntegrationTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2019 the original author or authors.
2+
* Copyright 2013-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,12 +15,12 @@
1515
*/
1616
package org.springsource.restbucks.order.web;
1717

18-
import static org.hamcrest.CoreMatchers.*;
18+
import static org.assertj.core.api.Assertions.*;
1919
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
20-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
2120

2221
import org.junit.jupiter.api.Test;
2322
import org.springframework.hateoas.MediaTypes;
23+
import org.springframework.http.HttpStatus;
2424
import org.springsource.restbucks.AbstractWebIntegrationTest;
2525

2626
/**
@@ -33,9 +33,10 @@ class OrderResourceIntegrationTest extends AbstractWebIntegrationTest {
3333
@Test
3434
void exposesOrdersResourceViaRootResource() throws Exception {
3535

36-
mvc.perform(get("/")) //
37-
.andExpect(status().isOk()) //
38-
.andExpect(content().contentTypeCompatibleWith(MediaTypes.HAL_JSON)) //
39-
.andExpect(jsonPath("$._links.restbucks:orders.href", notNullValue()));
36+
var result = assertThat(mvc.perform(get("/"))); //
37+
38+
result.hasStatus(HttpStatus.OK);
39+
result.contentType().isCompatibleWith(MediaTypes.HAL_JSON);
40+
result.body().jsonPath().extractingPath("$._links.restbucks:orders.href").isNotNull();
4041
}
4142
}

0 commit comments

Comments
 (0)