Polish AssertJ support for MockMvc

See gh-21178
This commit is contained in:
Sam Brannen
2024-03-15 17:15:32 +01:00
parent 4a74e1fc2d
commit cf31d088e2
31 changed files with 276 additions and 258 deletions

View File

@@ -208,7 +208,8 @@ class JsonPathAssertTests {
@Test
void convertToWithoutHttpMessageConverterShouldFail() {
JsonPathValueAssert path = assertThat(forJson(SIMPSONS)).extractingPath("$.familyMembers[0]");
assertThatIllegalStateException().isThrownBy(() -> path.convertTo(Member.class))
assertThatIllegalStateException()
.isThrownBy(() -> path.convertTo(Member.class))
.withMessage("No JSON message converter available to convert {name=Homer}");
}
@@ -296,7 +297,7 @@ class JsonPathAssertTests {
private Consumer<AssertionError> hasFailedToNotMatchPath(String expression) {
return error -> assertThat(error.getMessage()).containsSubsequence("Expecting:",
"To not match JSON path:", "\"" + expression + "\"");
"Not to match JSON path:", "\"" + expression + "\"");
}

View File

@@ -51,21 +51,21 @@ class UriAssertTests {
}
@Test
void matchPattern() {
assertThat("/orders/1").matchPattern("/orders/*");
void matchesPattern() {
assertThat("/orders/1").matchesPattern("/orders/*");
}
@Test
void matchPatternWithNonValidPattern() {
void matchesPatternWithNonValidPattern() {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat("/orders/1").matchPattern("/orders/"))
.isThrownBy(() -> assertThat("/orders/1").matchesPattern("/orders/"))
.withMessage("'/orders/' is not an Ant-style path pattern");
}
@Test
void matchPatternWithWrongValue() {
void matchesPatternWithWrongValue() {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat("/orders/1").matchPattern("/resources/*"))
.isThrownBy(() -> assertThat("/orders/1").matchesPattern("/resources/*"))
.withMessageContainingAll("Test URI", "/resources/*", "/orders/1");
}

View File

@@ -28,6 +28,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import static java.util.Map.entry;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link AbstractHttpServletRequestAssert}.
@@ -52,7 +53,7 @@ public class AbstractHttpServletRequestAssertTests {
@Test
void attributesWithWrongKey() {
HttpServletRequest request = createRequest(Map.of("one", 1));
Assertions.assertThatExceptionOfType(AssertionError.class)
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(request).attributes().containsKey("two"))
.withMessageContainingAll("Request Attributes", "two", "one");
}
@@ -80,7 +81,7 @@ public class AbstractHttpServletRequestAssertTests {
@Test
void sessionAttributesWithWrongKey() {
HttpServletRequest request = createRequest(Map.of("one", 1));
Assertions.assertThatExceptionOfType(AssertionError.class)
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(request).sessionAttributes().containsKey("two"))
.withMessageContainingAll("Session Attributes", "two", "one");
}
@@ -107,7 +108,7 @@ public class AbstractHttpServletRequestAssertTests {
void hasAsyncStartedTrueWithFalse() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setAsyncStarted(false);
Assertions.assertThatExceptionOfType(AssertionError.class)
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(request).hasAsyncStarted(true))
.withMessage("Async expected to have started");
}
@@ -123,12 +124,13 @@ public class AbstractHttpServletRequestAssertTests {
void hasAsyncStartedFalseWithTrue() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setAsyncStarted(true);
Assertions.assertThatExceptionOfType(AssertionError.class)
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(request).hasAsyncStarted(false))
.withMessage("Async expected to not have started");
.withMessage("Async expected not to have started");
}
private static ResponseAssert assertThat(HttpServletRequest response) {
return new ResponseAssert(response);
}
@@ -140,4 +142,5 @@ public class AbstractHttpServletRequestAssertTests {
super(actual, ResponseAssert.class);
}
}
}

View File

@@ -73,7 +73,8 @@ class AbstractHttpServletResponseAssertTests {
@Test
void hasStatusWithWrongCode() {
MockHttpServletResponse response = createResponse(200);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(response).hasStatus(300))
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(response).hasStatus(300))
.withMessageContainingAll("HTTP status code", "200", "300");
}
@@ -117,6 +118,7 @@ class AbstractHttpServletResponseAssertTests {
}
}
private static ResponseAssert assertThat(HttpServletResponse response) {
return new ResponseAssert(response);
}

View File

@@ -18,14 +18,15 @@ package org.springframework.test.web.servlet.assertj;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.mock.web.MockHttpServletResponse;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link AbstractMockHttpServletResponseAssert}.
*
@@ -33,10 +34,12 @@ import org.springframework.mock.web.MockHttpServletResponse;
*/
public class AbstractMockHttpServletResponseAssertTests {
private MockHttpServletResponse response = new MockHttpServletResponse();
@Test
void hasForwardedUrl() {
String forwardedUrl = "https://example.com/42";
MockHttpServletResponse response = new MockHttpServletResponse();
response.setForwardedUrl(forwardedUrl);
assertThat(response).hasForwardedUrl(forwardedUrl);
}
@@ -44,9 +47,8 @@ public class AbstractMockHttpServletResponseAssertTests {
@Test
void hasForwardedUrlWithWrongValue() {
String forwardedUrl = "https://example.com/42";
MockHttpServletResponse response = new MockHttpServletResponse();
response.setForwardedUrl(forwardedUrl);
Assertions.assertThatExceptionOfType(AssertionError.class)
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(response).hasForwardedUrl("another"))
.withMessageContainingAll("Forwarded URL", forwardedUrl, "another");
}
@@ -54,7 +56,6 @@ public class AbstractMockHttpServletResponseAssertTests {
@Test
void hasRedirectedUrl() {
String redirectedUrl = "https://example.com/42";
MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader(HttpHeaders.LOCATION, redirectedUrl);
assertThat(response).hasRedirectedUrl(redirectedUrl);
}
@@ -62,26 +63,23 @@ public class AbstractMockHttpServletResponseAssertTests {
@Test
void hasRedirectedUrlWithWrongValue() {
String redirectedUrl = "https://example.com/42";
MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader(HttpHeaders.LOCATION, redirectedUrl);
Assertions.assertThatExceptionOfType(AssertionError.class)
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(response).hasRedirectedUrl("another"))
.withMessageContainingAll("Redirected URL", redirectedUrl, "another");
}
@Test
void bodyHasContent() throws UnsupportedEncodingException {
MockHttpServletResponse response = new MockHttpServletResponse();
response.getWriter().write("OK");
assertThat(response).body().asString().isEqualTo("OK");
}
@Test
void bodyHasContentWithResponseCharacterEncoding() throws UnsupportedEncodingException {
byte[] bytes = "OK".getBytes(StandardCharsets.UTF_8);
MockHttpServletResponse response = new MockHttpServletResponse();
byte[] bytes = "OK".getBytes(UTF_8);
response.getWriter().write("OK");
response.setContentType(StandardCharsets.UTF_8.name());
response.setContentType(UTF_8.name());
assertThat(response).body().isEqualTo(bytes);
}
@@ -103,4 +101,5 @@ public class AbstractMockHttpServletResponseAssertTests {
}
}
}

View File

@@ -309,15 +309,15 @@ public class AssertableMockMvcIntegrationTests {
@Test
void doesNotHaveUnresolvedExceptionWithUnresolvedException() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertThat(perform(get("/error/1"))).doesNotHaveUnresolvedException())
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(perform(get("/error/1"))).doesNotHaveUnresolvedException())
.withMessage("Expecting request to have succeeded but it has failed");
}
@Test
void hasUnresolvedExceptionWithoutUnresolvedException() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertThat(perform(get("/greet"))).hasUnresolvedException())
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(perform(get("/greet"))).hasUnresolvedException())
.withMessage("Expecting request to have failed but it has succeeded");
}
@@ -330,8 +330,8 @@ public class AssertableMockMvcIntegrationTests {
@Test
void unresolvedExceptionWithSuccessfulRequest() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertThat(perform(get("/greet"))).unresolvedException())
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(perform(get("/greet"))).unresolvedException())
.withMessage("Expecting request to have failed but it has succeeded");
}
@@ -439,7 +439,7 @@ public class AssertableMockMvcIntegrationTests {
}
@Test
void satisfiesAllowAdditionalAssertions() {
void satisfiesAllowsAdditionalAssertions() {
assertThat(this.mockMvc.perform(get("/greet"))).satisfies(result -> {
assertThat(result).isInstanceOf(MvcResult.class);
assertThat(result).hasStatusOk();
@@ -477,7 +477,6 @@ public class AssertableMockMvcIntegrationTests {
@Import({ TestController.class, PersonController.class, AsyncController.class,
SessionController.class, ErrorController.class })
static class WebConfiguration {
}
@RestController
@@ -515,7 +514,6 @@ public class AssertableMockMvcIntegrationTests {
}
}
@RestController
static class AsyncController {
@@ -552,7 +550,6 @@ public class AssertableMockMvcIntegrationTests {
public String validation(@PathVariable @Size(max = 4) String id) {
return "Hello " + id;
}
}
}

View File

@@ -29,7 +29,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.GenericHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.json.JsonPathAssert;
@@ -40,8 +39,8 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@@ -58,10 +57,10 @@ class AssertableMockMvcTests {
private static final MappingJackson2HttpMessageConverter jsonHttpMessageConverter =
new MappingJackson2HttpMessageConverter(new ObjectMapper());
@Test
void createShouldRejectNullMockMvc() {
assertThatThrownBy(() -> AssertableMockMvc.create(null))
.isInstanceOf(IllegalArgumentException.class);
assertThatIllegalArgumentException().isThrownBy(() -> AssertableMockMvc.create(null));
}
@Test
@@ -122,8 +121,7 @@ class AssertableMockMvcTests {
void withHttpMessageConverterDetectsJsonConverter() {
MappingJackson2HttpMessageConverter converter = spy(jsonHttpMessageConverter);
AssertableMockMvc mockMvc = AssertableMockMvc.of(HelloController.class)
.withHttpMessageConverters(List.of(mock(GenericHttpMessageConverter.class),
mock(GenericHttpMessageConverter.class), converter));
.withHttpMessageConverters(List.of(mock(), mock(), converter));
assertThat(mockMvc.perform(get("/json"))).hasStatusOk().body().jsonPath()
.extractingPath("$").convertTo(Message.class).satisfies(message -> {
assertThat(message.message()).isEqualTo("Hello World");
@@ -136,14 +134,13 @@ class AssertableMockMvcTests {
void performWithUnresolvedExceptionSetsException() {
AssertableMockMvc mockMvc = AssertableMockMvc.of(HelloController.class);
AssertableMvcResult result = mockMvc.perform(get("/error"));
assertThat(result.getUnresolvedException()).isNotNull().isInstanceOf(ServletException.class)
assertThat(result.getUnresolvedException()).isInstanceOf(ServletException.class)
.cause().isInstanceOf(IllegalStateException.class).hasMessage("Expected");
assertThat(result).hasFieldOrPropertyWithValue("target", null);
}
private GenericWebApplicationContext create(Class<?>... classes) {
GenericWebApplicationContext applicationContext = new GenericWebApplicationContext(
new MockServletContext());
GenericWebApplicationContext applicationContext = new GenericWebApplicationContext(new MockServletContext());
AnnotationConfigUtils.registerAnnotationConfigProcessors(applicationContext);
for (Class<?> beanClass : classes) {
applicationContext.registerBean(beanClass);
@@ -189,22 +186,23 @@ class AssertableMockMvcTests {
private record Message(String message, int counter) {}
@RestController
private static class CounterController {
static class CounterController {
private final AtomicInteger counter;
public CounterController(AtomicInteger counter) {
this.counter = counter;
}
public CounterController() {
CounterController() {
this(new AtomicInteger());
}
CounterController(AtomicInteger counter) {
this.counter = counter;
}
@PostMapping("/increase")
public String increase() {
String increase() {
int value = this.counter.incrementAndGet();
return "counter " + value;
}
}
}

View File

@@ -16,7 +16,6 @@
package org.springframework.test.web.servlet.assertj;
import java.time.Duration;
import java.util.List;
@@ -53,130 +52,130 @@ class CookieMapAssertTests {
@Test
void containsCookieWhenCookieExistsShouldPass() {
assertThat(forCookies()).containsCookie("framework");
cookies().containsCookie("framework");
}
@Test
void containsCookieWhenCookieMissingShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertThat(forCookies()).containsCookie("missing"));
cookies().containsCookie("missing"));
}
@Test
void containsCookiesWhenCookiesExistShouldPass() {
assertThat(forCookies()).containsCookies("framework", "age");
cookies().containsCookies("framework", "age");
}
@Test
void containsCookiesWhenCookieMissingShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertThat(forCookies()).containsCookies("framework", "missing"));
cookies().containsCookies("framework", "missing"));
}
@Test
void doesNotContainCookieWhenCookieMissingShouldPass() {
assertThat(forCookies()).doesNotContainCookie("missing");
cookies().doesNotContainCookie("missing");
}
@Test
void doesNotContainCookieWhenCookieExistsShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertThat(forCookies()).doesNotContainCookie("framework"));
cookies().doesNotContainCookie("framework"));
}
@Test
void doesNotContainCookiesWhenCookiesMissingShouldPass() {
assertThat(forCookies()).doesNotContainCookies("missing", "missing2");
cookies().doesNotContainCookies("missing", "missing2");
}
@Test
void doesNotContainCookiesWhenAtLeastOneCookieExistShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertThat(forCookies()).doesNotContainCookies("missing", "framework"));
cookies().doesNotContainCookies("missing", "framework"));
}
@Test
void hasValueEqualsWhenCookieValueMatchesShouldPass() {
assertThat(forCookies()).hasValue("framework", "spring");
cookies().hasValue("framework", "spring");
}
@Test
void hasValueEqualsWhenCookieValueDiffersShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertThat(forCookies()).hasValue("framework", "other"));
cookies().hasValue("framework", "other"));
}
@Test
void hasCookieSatisfyingWhenCookieValueMatchesShouldPass() {
assertThat(forCookies()).hasCookieSatisfying("framework", cookie ->
cookies().hasCookieSatisfying("framework", cookie ->
assertThat(cookie.getValue()).startsWith("spr"));
}
@Test
void hasCookieSatisfyingWhenCookieValueDiffersShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertThat(forCookies()).hasCookieSatisfying("framework", cookie ->
cookies().hasCookieSatisfying("framework", cookie ->
assertThat(cookie.getValue()).startsWith("not")));
}
@Test
void hasMaxAgeWhenCookieAgeMatchesShouldPass() {
assertThat(forCookies()).hasMaxAge("age", Duration.ofMinutes(20));
cookies().hasMaxAge("age", Duration.ofMinutes(20));
}
@Test
void hasMaxAgeWhenCookieAgeDiffersShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertThat(forCookies()).hasMaxAge("age", Duration.ofMinutes(30)));
cookies().hasMaxAge("age", Duration.ofMinutes(30)));
}
@Test
void pathWhenCookiePathMatchesShouldPass() {
assertThat(forCookies()).hasPath("path", "/spring");
cookies().hasPath("path", "/spring");
}
@Test
void pathWhenCookiePathDiffersShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertThat(forCookies()).hasPath("path", "/other"));
cookies().hasPath("path", "/other"));
}
@Test
void hasDomainWhenCookieDomainMatchesShouldPass() {
assertThat(forCookies()).hasDomain("domain", "spring.io");
cookies().hasDomain("domain", "spring.io");
}
@Test
void hasDomainWhenCookieDomainDiffersShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertThat(forCookies()).hasDomain("domain", "example.org"));
cookies().hasDomain("domain", "example.org"));
}
@Test
void isSecureWhenCookieSecureMatchesShouldPass() {
assertThat(forCookies()).isSecure("framework", true);
cookies().isSecure("framework", true);
}
@Test
void isSecureWhenCookieSecureDiffersShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertThat(forCookies()).isSecure("domain", true));
cookies().isSecure("domain", true));
}
@Test
void isHttpOnlyWhenCookieHttpOnlyMatchesShouldPass() {
assertThat(forCookies()).isHttpOnly("framework", true);
cookies().isHttpOnly("framework", true);
}
@Test
void isHttpOnlyWhenCookieHttpOnlyDiffersShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertThat(forCookies()).isHttpOnly("domain", true));
cookies().isHttpOnly("domain", true));
}
private AssertProvider<CookieMapAssert> forCookies() {
return () -> new CookieMapAssert(cookies);
private static CookieMapAssert cookies() {
return assertThat((AssertProvider<CookieMapAssert>) () -> new CookieMapAssert(cookies));
}
}

View File

@@ -71,8 +71,7 @@ class HandlerResultAssertTests {
@Test
void method() {
assertThat(handlerMethod(new TestController(), "greet")).method().isEqualTo(
ReflectionUtils.findMethod(TestController.class, "greet"));
assertThat(handlerMethod(new TestController(), "greet")).method().isEqualTo(method(TestController.class, "greet"));
}
@Test
@@ -126,7 +125,7 @@ class HandlerResultAssertTests {
}
@RestController
public static class TestController {
static class TestController {
@GetMapping("/greet")
public ResponseEntity<String> greet() {

View File

@@ -45,7 +45,8 @@ class ModelAssertTests {
@Test
void hasErrorsWithNoError() {
AssertProvider<ModelAssert> actual = forModel(new TestBean(), Map.of("name", "John", "age", "42"));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(actual).hasErrors())
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(actual).hasErrors())
.withMessageContainingAll("John", "to have at least one error");
}
@@ -57,7 +58,8 @@ class ModelAssertTests {
@Test
void doesNotHaveErrorsWithError() {
AssertProvider<ModelAssert> actual = forModel(new TestBean(), Map.of("name", "John", "age", "4x"));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(actual).doesNotHaveErrors())
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(actual).doesNotHaveErrors())
.withMessageContainingAll("John", "to not have an error, but got 1");
}
@@ -73,8 +75,8 @@ class ModelAssertTests {
Map<String, Object> model = new HashMap<>();
augmentModel(model, "person", new TestBean(), Map.of("name", "John", "age", "42"));
AssertProvider<ModelAssert> actual = forModel(model);
assertThatExceptionOfType(AssertionError.class).isThrownBy(
() -> assertThat(actual).extractingBindingResult("user"))
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(actual).extractingBindingResult("user"))
.withMessageContainingAll("to have a binding result for attribute 'user'");
}
@@ -94,10 +96,10 @@ class ModelAssertTests {
augmentModel(model, "valid", new TestBean(), Map.of("name", "second"));
augmentModel(model, "wrong2", new TestBean(), Map.of("name", "third", "touchy", "invalid.name"));
AssertProvider<ModelAssert> actual = forModel(model);
assertThatExceptionOfType(AssertionError.class).isThrownBy(
() -> assertThat(actual).hasAttributeErrors("wrong1", "valid"))
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(actual).hasAttributeErrors("wrong1", "valid"))
.withMessageContainingAll("to have attribute errors for:", "wrong1, valid",
"but these attributes do not have any error:", "valid");
"but these attributes do not have any errors:", "valid");
}
@Test
@@ -107,11 +109,11 @@ class ModelAssertTests {
augmentModel(model, "valid", new TestBean(), Map.of("name", "second"));
augmentModel(model, "wrong2", new TestBean(), Map.of("name", "third", "touchy", "invalid.name"));
AssertProvider<ModelAssert> actual = forModel(model);
assertThatExceptionOfType(AssertionError.class).isThrownBy(
() -> assertThat(actual).hasAttributeErrors("wrong1", "unknown", "valid"))
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(actual).hasAttributeErrors("wrong1", "unknown", "valid"))
.withMessageContainingAll("to have attribute errors for:", "wrong1, unknown, valid",
"but could not find these attributes:", "unknown",
"and these attributes do not have any error:", "valid");
"and these attributes do not have any errors:", "valid");
}
@Test
@@ -130,10 +132,10 @@ class ModelAssertTests {
augmentModel(model, "wrong", new TestBean(), Map.of("name", "second", "age", "4x"));
augmentModel(model, "valid2", new TestBean(), Map.of("name", "third"));
AssertProvider<ModelAssert> actual = forModel(model);
assertThatExceptionOfType(AssertionError.class).isThrownBy(
() -> assertThat(actual).doesNotHaveAttributeErrors("valid1", "wrong"))
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(actual).doesNotHaveAttributeErrors("valid1", "wrong"))
.withMessageContainingAll("to have attribute without errors for:", "valid1, wrong",
"but these attributes have at least an error:", "wrong");
"but these attributes have at least one error:", "wrong");
}
@Test
@@ -143,11 +145,11 @@ class ModelAssertTests {
augmentModel(model, "wrong", new TestBean(), Map.of("name", "second", "age", "4x"));
augmentModel(model, "valid2", new TestBean(), Map.of("name", "third"));
AssertProvider<ModelAssert> actual = forModel(model);
assertThatExceptionOfType(AssertionError.class).isThrownBy(
() -> assertThat(actual).doesNotHaveAttributeErrors("valid1", "unknown", "wrong"))
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(actual).doesNotHaveAttributeErrors("valid1", "unknown", "wrong"))
.withMessageContainingAll("to have attribute without errors for:", "valid1, unknown, wrong",
"but could not find these attributes:", "unknown",
"and these attributes have at least an error:", "wrong");
"and these attributes have at least one error:", "wrong");
}
private AssertProvider<ModelAssert> forModel(Map<String, Object> model) {

View File

@@ -19,7 +19,6 @@ package org.springframework.test.web.servlet.assertj;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.assertj.core.api.AssertProvider;
import org.junit.jupiter.api.Test;
@@ -27,6 +26,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.json.JsonContent;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
/**
@@ -40,8 +40,8 @@ class ResponseBodyAssertTests {
@Test
void isEqualToWithByteArray() {
MockHttpServletResponse response = createResponse("hello");
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
assertThat(fromResponse(response)).isEqualTo("hello".getBytes(StandardCharsets.UTF_8));
response.setCharacterEncoding(UTF_8.name());
assertThat(fromResponse(response)).isEqualTo("hello".getBytes(UTF_8));
}
@Test