Use AssertJ's doesNotContainKey

Closes gh-24970
This commit is contained in:
Andy Wilkinson 2021-01-22 13:28:41 +00:00
parent 244a6ac7e5
commit 0b06ac9938
4 changed files with 36 additions and 35 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -84,13 +84,13 @@ class BasicErrorControllerIntegrationTests {
} }
@Test @Test
@SuppressWarnings("rawtypes") @SuppressWarnings({ "rawtypes", "unchecked" })
void testErrorForMachineClientDefault() { void testErrorForMachineClientDefault() {
load(); load();
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("?trace=true"), Map.class); ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("?trace=true"), Map.class);
assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", null, "", "/"); assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", null, "", "/");
assertThat(entity.getBody().containsKey("exception")).isFalse(); assertThat(entity.getBody()).doesNotContainKey("exception");
assertThat(entity.getBody().containsKey("trace")).isFalse(); assertThat(entity.getBody()).doesNotContainKey("trace");
} }
@Test @Test
@ -136,6 +136,7 @@ class BasicErrorControllerIntegrationTests {
} }
@Test @Test
@SuppressWarnings("rawtypes")
void testErrorForMachineClientAlwaysParamsWithoutMessage() { void testErrorForMachineClientAlwaysParamsWithoutMessage() {
load("--server.error.include-exception=true", "--server.error.include-message=always"); load("--server.error.include-exception=true", "--server.error.include-message=always");
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/noMessage"), Map.class); ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/noMessage"), Map.class);
@ -151,11 +152,11 @@ class BasicErrorControllerIntegrationTests {
assertThat(entity.getBody().containsKey("trace")).isTrue(); assertThat(entity.getBody().containsKey("trace")).isTrue();
} }
@SuppressWarnings("rawtypes") @SuppressWarnings({ "rawtypes", "unchecked" })
private void exceptionWithoutStackTraceAndMessage(String path) { private void exceptionWithoutStackTraceAndMessage(String path) {
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl(path), Map.class); ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl(path), Map.class);
assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", IllegalStateException.class, "", "/"); assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", IllegalStateException.class, "", "/");
assertThat(entity.getBody().containsKey("trace")).isFalse(); assertThat(entity.getBody()).doesNotContainKey("trace");
} }
@Test @Test
@ -271,30 +272,30 @@ class BasicErrorControllerIntegrationTests {
assertThat(entity.getBody().containsKey("errors")).isTrue(); assertThat(entity.getBody().containsKey("errors")).isTrue();
} }
@SuppressWarnings({ "rawtypes" }) @SuppressWarnings({ "rawtypes", "unchecked" })
private void bindingExceptionWithoutErrors(String param) { private void bindingExceptionWithoutErrors(String param) {
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class); ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class);
assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class, "", "/bind"); assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class, "", "/bind");
assertThat(entity.getBody().containsKey("errors")).isFalse(); assertThat(entity.getBody()).doesNotContainKey("errors");
} }
@SuppressWarnings({ "rawtypes" }) @SuppressWarnings({ "rawtypes", "unchecked" })
private void bindingExceptionWithMessage(String param) { private void bindingExceptionWithMessage(String param) {
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class); ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class);
assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class, assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class,
"Validation failed for object='test'. Error count: 1", "/bind"); "Validation failed for object='test'. Error count: 1", "/bind");
assertThat(entity.getBody().containsKey("errors")).isFalse(); assertThat(entity.getBody()).doesNotContainKey("errors");
} }
@SuppressWarnings({ "rawtypes" }) @SuppressWarnings({ "rawtypes", "unchecked" })
private void bindingExceptionWithoutMessage(String param) { private void bindingExceptionWithoutMessage(String param) {
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class); ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class);
assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class, "", "/bind"); assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class, "", "/bind");
assertThat(entity.getBody().containsKey("errors")).isFalse(); assertThat(entity.getBody()).doesNotContainKey("errors");
} }
@Test @Test
@SuppressWarnings("rawtypes") @SuppressWarnings({ "rawtypes", "unchecked" })
void testRequestBodyValidationForMachineClient() { void testRequestBodyValidationForMachineClient() {
load("--server.error.include-exception=true"); load("--server.error.include-exception=true");
RequestEntity request = RequestEntity.post(URI.create(createUrl("/bodyValidation"))) RequestEntity request = RequestEntity.post(URI.create(createUrl("/bodyValidation")))
@ -302,19 +303,19 @@ class BasicErrorControllerIntegrationTests {
ResponseEntity<Map> entity = new TestRestTemplate().exchange(request, Map.class); ResponseEntity<Map> entity = new TestRestTemplate().exchange(request, Map.class);
assertErrorAttributes(entity.getBody(), "400", "Bad Request", MethodArgumentNotValidException.class, "", assertErrorAttributes(entity.getBody(), "400", "Bad Request", MethodArgumentNotValidException.class, "",
"/bodyValidation"); "/bodyValidation");
assertThat(entity.getBody().containsKey("errors")).isFalse(); assertThat(entity.getBody()).doesNotContainKey("errors");
} }
@Test @Test
@SuppressWarnings("rawtypes") @SuppressWarnings({ "rawtypes", "unchecked" })
void testBindingExceptionForMachineClientDefault() { void testBindingExceptionForMachineClientDefault() {
load(); load();
RequestEntity request = RequestEntity.get(URI.create(createUrl("/bind?trace=true,message=true"))) RequestEntity request = RequestEntity.get(URI.create(createUrl("/bind?trace=true,message=true")))
.accept(MediaType.APPLICATION_JSON).build(); .accept(MediaType.APPLICATION_JSON).build();
ResponseEntity<Map> entity = new TestRestTemplate().exchange(request, Map.class); ResponseEntity<Map> entity = new TestRestTemplate().exchange(request, Map.class);
assertThat(entity.getBody().containsKey("exception")).isFalse(); assertThat(entity.getBody()).doesNotContainKey("exception");
assertThat(entity.getBody().containsKey("trace")).isFalse(); assertThat(entity.getBody()).doesNotContainKey("trace");
assertThat(entity.getBody().containsKey("errors")).isFalse(); assertThat(entity.getBody()).doesNotContainKey("errors");
} }
@Test @Test

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -82,7 +82,7 @@ class FileSessionPersistenceTests {
this.persistence.persistSessions("test", sessionData); this.persistence.persistSessions("test", sessionData);
Map<String, PersistentSession> restored = this.persistence.loadSessionAttributes("test", this.classLoader); Map<String, PersistentSession> restored = this.persistence.loadSessionAttributes("test", this.classLoader);
assertThat(restored).isNotNull(); assertThat(restored).isNotNull();
assertThat(restored.containsKey("abc")).isFalse(); assertThat(restored).doesNotContainKey("abc");
} }
@Test @Test

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -269,8 +269,8 @@ class DefaultErrorAttributesTests {
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, ex), Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, ex),
ErrorAttributeOptions.defaults()); ErrorAttributeOptions.defaults());
assertThat(attributes.get("message")).isEqualTo(""); assertThat(attributes).containsEntry("message", "");
assertThat(attributes.containsKey("errors")).isFalse(); assertThat(attributes).doesNotContainKey("errors");
} }
private ServerRequest buildServerRequest(MockServerHttpRequest request, Throwable error) { private ServerRequest buildServerRequest(MockServerHttpRequest request, Throwable error) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -90,7 +90,7 @@ class DefaultErrorAttributesTests {
ErrorAttributeOptions.of(Include.MESSAGE)); ErrorAttributeOptions.of(Include.MESSAGE));
assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(ex); assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(ex);
assertThat(modelAndView).isNull(); assertThat(modelAndView).isNull();
assertThat(attributes.containsKey("exception")).isFalse(); assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes.get("message")).isEqualTo("Test"); assertThat(attributes.get("message")).isEqualTo("Test");
} }
@ -101,7 +101,7 @@ class DefaultErrorAttributesTests {
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest, Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest,
ErrorAttributeOptions.of(Include.MESSAGE)); ErrorAttributeOptions.of(Include.MESSAGE));
assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(ex); assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(ex);
assertThat(attributes.containsKey("exception")).isFalse(); assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes.get("message")).isEqualTo("Test"); assertThat(attributes.get("message")).isEqualTo("Test");
} }
@ -112,7 +112,7 @@ class DefaultErrorAttributesTests {
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest, Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest,
ErrorAttributeOptions.defaults()); ErrorAttributeOptions.defaults());
assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(ex); assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(ex);
assertThat(attributes.containsKey("exception")).isFalse(); assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes.get("message").toString()).contains(""); assertThat(attributes.get("message").toString()).contains("");
} }
@ -121,7 +121,7 @@ class DefaultErrorAttributesTests {
this.request.setAttribute("javax.servlet.error.message", "Test"); this.request.setAttribute("javax.servlet.error.message", "Test");
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest, Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest,
ErrorAttributeOptions.of(Include.MESSAGE)); ErrorAttributeOptions.of(Include.MESSAGE));
assertThat(attributes.containsKey("exception")).isFalse(); assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes.get("message")).isEqualTo("Test"); assertThat(attributes.get("message")).isEqualTo("Test");
} }
@ -130,7 +130,7 @@ class DefaultErrorAttributesTests {
this.request.setAttribute("javax.servlet.error.message", "Test"); this.request.setAttribute("javax.servlet.error.message", "Test");
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest, Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest,
ErrorAttributeOptions.defaults()); ErrorAttributeOptions.defaults());
assertThat(attributes.containsKey("exception")).isFalse(); assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes.get("message")).asString().contains(""); assertThat(attributes.get("message")).asString().contains("");
} }
@ -140,7 +140,7 @@ class DefaultErrorAttributesTests {
this.request.setAttribute("javax.servlet.error.message", "Test"); this.request.setAttribute("javax.servlet.error.message", "Test");
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest, Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest,
ErrorAttributeOptions.of(Include.MESSAGE)); ErrorAttributeOptions.of(Include.MESSAGE));
assertThat(attributes.containsKey("exception")).isFalse(); assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes.get("message")).isEqualTo("Test"); assertThat(attributes.get("message")).isEqualTo("Test");
} }
@ -149,7 +149,7 @@ class DefaultErrorAttributesTests {
this.request.setAttribute("javax.servlet.error.exception", new RuntimeException()); this.request.setAttribute("javax.servlet.error.exception", new RuntimeException());
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest, Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest,
ErrorAttributeOptions.of(Include.MESSAGE)); ErrorAttributeOptions.of(Include.MESSAGE));
assertThat(attributes.containsKey("exception")).isFalse(); assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes.get("message")).isEqualTo("No message available"); assertThat(attributes.get("message")).isEqualTo("No message available");
} }
@ -161,7 +161,7 @@ class DefaultErrorAttributesTests {
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest, Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest,
ErrorAttributeOptions.of(Include.MESSAGE)); ErrorAttributeOptions.of(Include.MESSAGE));
assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(wrapped); assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(wrapped);
assertThat(attributes.containsKey("exception")).isFalse(); assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes.get("message")).isEqualTo("Test"); assertThat(attributes.get("message")).isEqualTo("Test");
} }
@ -172,7 +172,7 @@ class DefaultErrorAttributesTests {
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest, Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest,
ErrorAttributeOptions.of(Include.MESSAGE)); ErrorAttributeOptions.of(Include.MESSAGE));
assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(error); assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(error);
assertThat(attributes.containsKey("exception")).isFalse(); assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes.get("message")).isEqualTo("Test error"); assertThat(attributes.get("message")).isEqualTo("Test error");
} }
@ -216,7 +216,7 @@ class DefaultErrorAttributesTests {
assertThat(attributes.get("errors")).isEqualTo(bindingResult.getAllErrors()); assertThat(attributes.get("errors")).isEqualTo(bindingResult.getAllErrors());
} }
else { else {
assertThat(attributes.containsKey("errors")).isFalse(); assertThat(attributes).doesNotContainKey("errors");
} }
} }
@ -257,7 +257,7 @@ class DefaultErrorAttributesTests {
this.request.setAttribute("javax.servlet.error.exception", ex); this.request.setAttribute("javax.servlet.error.exception", ex);
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest, Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest,
ErrorAttributeOptions.defaults()); ErrorAttributeOptions.defaults());
assertThat(attributes.containsKey("trace")).isFalse(); assertThat(attributes).doesNotContainKey("trace");
} }
@Test @Test