diff --git a/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/ControllerIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/ControllerIntegrationTests.java index d12afba4c47..89db1afcbba 100644 --- a/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/ControllerIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/ControllerIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -85,8 +85,7 @@ class ControllerIntegrationTests { ApplicationContext parent = wac.getParent(); assertThat(parent).isNotNull(); - boolean condition = parent instanceof WebApplicationContext; - assertThat(condition).isTrue(); + assertThat(parent).isInstanceOf(WebApplicationContext.class); WebApplicationContext root = (WebApplicationContext) parent; assertThat(root.getBeansOfType(String.class).containsKey("bar")).isFalse(); diff --git a/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/DispatcherWacRootWacEarTests.java b/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/DispatcherWacRootWacEarTests.java index b4485ebf1ed..9b0ec5377de 100644 --- a/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/DispatcherWacRootWacEarTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/DispatcherWacRootWacEarTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -66,13 +66,11 @@ public class DispatcherWacRootWacEarTests extends RootWacEarTests { void verifyDispatcherWacConfig() { ApplicationContext parent = wac.getParent(); assertThat(parent).isNotNull(); - boolean condition = parent instanceof WebApplicationContext; - assertThat(condition).isTrue(); + assertThat(parent).isInstanceOf(WebApplicationContext.class); ApplicationContext grandParent = parent.getParent(); assertThat(grandParent).isNotNull(); - boolean condition1 = grandParent instanceof WebApplicationContext; - assertThat(condition1).isFalse(); + assertThat(grandParent).isNotInstanceOf(WebApplicationContext.class); ServletContext dispatcherServletContext = wac.getServletContext(); assertThat(dispatcherServletContext).isNotNull(); diff --git a/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/EarTests.java b/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/EarTests.java index bf360dce957..e20d58320b1 100644 --- a/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/EarTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/EarTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,8 +58,7 @@ class EarTests { @Test void verifyEarConfig() { - boolean condition = context instanceof WebApplicationContext; - assertThat(condition).isFalse(); + assertThat(context).isNotInstanceOf(WebApplicationContext.class); assertThat(context.getParent()).isNull(); assertThat(ear).isEqualTo("ear"); } diff --git a/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/RootWacEarTests.java b/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/RootWacEarTests.java index 1e89e0735c0..d53f9b10afd 100644 --- a/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/RootWacEarTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/RootWacEarTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -71,8 +71,7 @@ class RootWacEarTests extends EarTests { void verifyRootWacConfig() { ApplicationContext parent = wac.getParent(); assertThat(parent).isNotNull(); - boolean condition = parent instanceof WebApplicationContext; - assertThat(condition).isFalse(); + assertThat(parent).isNotInstanceOf(WebApplicationContext.class); assertThat(ear).isEqualTo("ear"); assertThat(root).isEqualTo("root"); } diff --git a/spring-test/src/test/java/org/springframework/test/web/client/response/ResponseCreatorsTests.java b/spring-test/src/test/java/org/springframework/test/web/client/response/ResponseCreatorsTests.java index 9b995c616c0..a8641cf317a 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/response/ResponseCreatorsTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/response/ResponseCreatorsTests.java @@ -45,7 +45,7 @@ class ResponseCreatorsTests { MockClientHttpResponse response = (MockClientHttpResponse) MockRestResponseCreators.withSuccess().createResponse(null); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); - assertThat(response.getHeaders().isEmpty()).isTrue(); + assertThat(response.getHeaders()).isEmpty(); assertThat(StreamUtils.copyToByteArray(response.getBody())).isEmpty(); } @@ -95,7 +95,7 @@ class ResponseCreatorsTests { MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT); - assertThat(response.getHeaders().isEmpty()).isTrue(); + assertThat(response.getHeaders()).isEmpty(); assertThat(StreamUtils.copyToByteArray(response.getBody())).isEmpty(); } @@ -105,7 +105,7 @@ class ResponseCreatorsTests { MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); - assertThat(response.getHeaders().isEmpty()).isTrue(); + assertThat(response.getHeaders()).isEmpty(); assertThat(StreamUtils.copyToByteArray(response.getBody())).isEmpty(); } @@ -115,7 +115,7 @@ class ResponseCreatorsTests { MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); - assertThat(response.getHeaders().isEmpty()).isTrue(); + assertThat(response.getHeaders()).isEmpty(); assertThat(StreamUtils.copyToByteArray(response.getBody())).isEmpty(); } @@ -172,7 +172,7 @@ class ResponseCreatorsTests { MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); - assertThat(response.getHeaders().isEmpty()).isTrue(); + assertThat(response.getHeaders()).isEmpty(); assertThat(StreamUtils.copyToByteArray(response.getBody())).isEmpty(); } @@ -209,7 +209,7 @@ class ResponseCreatorsTests { MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN); - assertThat(response.getHeaders().isEmpty()).isTrue(); + assertThat(response.getHeaders()).isEmpty(); assertThat(StreamUtils.copyToByteArray(response.getBody())).isEmpty(); } diff --git a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/ContentRequestMatchersIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/ContentRequestMatchersIntegrationTests.java index f2601e1eaf9..98ddc7110ae 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/ContentRequestMatchersIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/ContentRequestMatchersIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,21 +17,18 @@ package org.springframework.test.web.client.samples.matchers; import java.net.URI; -import java.net.URISyntaxException; -import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.test.web.Person; import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.web.client.RestTemplate; -import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.hamcrest.Matchers.startsWith; import static org.springframework.test.web.client.match.MockRestRequestMatchers.content; import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; @@ -40,68 +37,59 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat * Examples of defining expectations on request content and content type. * * @author Rossen Stoyanchev + * @author Sam Brannen * @see JsonPathRequestMatchersIntegrationTests * @see XmlContentRequestMatchersIntegrationTests * @see XpathRequestMatchersIntegrationTests */ -public class ContentRequestMatchersIntegrationTests { +class ContentRequestMatchersIntegrationTests { - private MockRestServiceServer mockServer; + private final RestTemplate restTemplate = new RestTemplate(); - private RestTemplate restTemplate; + private final MockRestServiceServer mockServer = MockRestServiceServer.createServer(this.restTemplate); @BeforeEach - public void setup() { - List> converters = new ArrayList<>(); - converters.add(new StringHttpMessageConverter()); - converters.add(new MappingJackson2HttpMessageConverter()); - - this.restTemplate = new RestTemplate(); - this.restTemplate.setMessageConverters(converters); - - this.mockServer = MockRestServiceServer.createServer(this.restTemplate); + void setup() { + this.restTemplate.setMessageConverters( + List.of(new StringHttpMessageConverter(), new MappingJackson2HttpMessageConverter())); } @Test - public void contentType() throws Exception { + void contentType() { this.mockServer.expect(content().contentType("application/json")).andRespond(withSuccess()); executeAndVerify(new Person()); } @Test - public void contentTypeNoMatch() throws Exception { + void contentTypeNoMatch() { this.mockServer.expect(content().contentType("application/json;charset=UTF-8")).andRespond(withSuccess()); - try { - executeAndVerify("foo"); - } - catch (AssertionError error) { - String message = error.getMessage(); - assertThat(message.startsWith("Content type expected:")).as(message).isTrue(); - } + assertThatExceptionOfType(AssertionError.class) + .isThrownBy(() -> executeAndVerify("foo")) + .withMessageStartingWith("Content type expected:"); } @Test - public void contentAsString() throws Exception { + void contentAsString() { this.mockServer.expect(content().string("foo")).andRespond(withSuccess()); executeAndVerify("foo"); } @Test - public void contentStringStartsWith() throws Exception { + void contentStringStartsWith() { this.mockServer.expect(content().string(startsWith("foo"))).andRespond(withSuccess()); executeAndVerify("foo123"); } @Test - public void contentAsBytes() throws Exception { + void contentAsBytes() { this.mockServer.expect(content().bytes("foo".getBytes())).andRespond(withSuccess()); executeAndVerify("foo"); } - private void executeAndVerify(Object body) throws URISyntaxException { - this.restTemplate.put(new URI("/foo"), body); + private void executeAndVerify(Object body) { + this.restTemplate.put(URI.create("/foo"), body); this.mockServer.verify(); } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java index 48646aa92a3..67bd61b2771 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -141,8 +141,7 @@ public class JavaConfigTests { ApplicationContext parent = wac.getParent(); assertThat(parent).isNotNull(); - boolean condition = parent instanceof WebApplicationContext; - assertThat(condition).isTrue(); + assertThat(parent).isInstanceOf(WebApplicationContext.class); WebApplicationContext root = (WebApplicationContext) parent; ServletContext childServletContext = wac.getServletContext();