From debe78b7f9329b6864440eb3d2f18717a2c0b263 Mon Sep 17 00:00:00 2001 From: Kulwant Singh Date: Tue, 1 Nov 2022 21:51:51 +0100 Subject: [PATCH] Minor improvements in spring-test - Remove final modifier from private method - Use StandardCharsets - Remove unnecessary toString calls - Remove unnecessary static modifiers - Refactor to use enhanced switch - Replace concatenated strings with text blocks - Rely on auto-boxing where appropriate - Remove unnecessary code - Fix imports in Kotlin test classes Closes gh-29413 --- ...AbstractExpressionEvaluatingCondition.java | 2 +- .../support/AbstractGenericContextLoader.java | 2 +- .../AbstractTestNGSpringContextTests.java | 2 +- .../test/util/XmlExpectationsHelper.java | 2 +- .../BootstrapWithTestInterface.java | 2 +- .../ContextConfigurationTestInterface.java | 2 +- .../WebAppConfigurationTestInterface.java | 2 +- ...eResolverWithCustomDefaultsMetaConfig.java | 8 +-- ...dProfilesWithCustomDefaultsMetaConfig.java | 4 +- .../ProgrammaticTxMgmtSpringRuleTests.java | 23 ++------- ...figClassesWithoutAtConfigurationTests.java | 2 +- .../DelegatingSmartContextLoaderTests.java | 2 +- .../ProgrammaticTxMgmtTestNGTests.java | 23 ++------- .../programmatic/ProgrammaticTxMgmtTests.java | 23 ++------- .../test/util/ReflectionTestUtilsTests.java | 50 +++++++++---------- .../client/MockRestServiceServerTests.java | 8 +-- .../SimpleRequestExpectationManagerTests.java | 47 ++++++++++------- ...orderedRequestExpectationManagerTests.java | 30 ++++++----- .../server/samples/SoftAssertionTests.java | 7 +-- .../DelegatingWebConnectionTests.java | 3 +- .../MockHttpServletRequestBuilderTests.java | 2 +- .../result/ContentResultMatchersTests.java | 6 +-- .../result/JsonPathResultMatchersTests.java | 4 +- .../result/PrintingResultHandlerTests.java | 9 ++-- .../result/StatusResultMatchersTests.java | 23 +++------ .../standalone/ExceptionHandlerTests.java | 13 ++--- .../resultmatches/XpathAssertionTests.java | 11 ++-- .../standalone/ExceptionHandlerTests.java | 13 ++--- .../standalone/MultipartControllerTests.java | 18 +++---- .../PrintingResultHandlerSmokeTests.java | 2 +- .../resultmatchers/ContentAssertionTests.java | 8 +-- .../resultmatchers/XpathAssertionTests.java | 11 ++-- .../web/servlet/MockMvcExtensionsTests.kt | 18 ++++--- 33 files changed, 175 insertions(+), 207 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/AbstractExpressionEvaluatingCondition.java b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/AbstractExpressionEvaluatingCondition.java index 22a9cfa8e3..aaa150b193 100644 --- a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/AbstractExpressionEvaluatingCondition.java +++ b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/AbstractExpressionEvaluatingCondition.java @@ -93,7 +93,7 @@ abstract class AbstractExpressionEvaluatingCondition implements ExecutionConditi AnnotatedElement element = context.getElement().get(); Optional annotation = findMergedAnnotation(element, annotationType); - if (!annotation.isPresent()) { + if (annotation.isEmpty()) { String reason = String.format("%s is enabled since @%s is not present", element, annotationType.getSimpleName()); if (logger.isDebugEnabled()) { diff --git a/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java b/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java index 5aa4d67dc6..17336b18bb 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java @@ -188,7 +188,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader * register a JVM shutdown hook for it * @return a new application context */ - private final GenericApplicationContext loadContext( + private GenericApplicationContext loadContext( MergedContextConfiguration mergedConfig, boolean forAotProcessing) throws Exception { if (logger.isTraceEnabled()) { diff --git a/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTestNGSpringContextTests.java b/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTestNGSpringContextTests.java index 269f82a069..aa334031f1 100644 --- a/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTestNGSpringContextTests.java +++ b/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTestNGSpringContextTests.java @@ -201,7 +201,7 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App private Throwable getTestResultException(ITestResult testResult) { Throwable testResultException = testResult.getThrowable(); if (testResultException instanceof InvocationTargetException) { - testResultException = ((InvocationTargetException) testResultException).getCause(); + testResultException = testResultException.getCause(); } return testResultException; } diff --git a/spring-test/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java b/spring-test/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java index 5241d57a05..ea2b95ec8d 100644 --- a/spring-test/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java +++ b/spring-test/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java @@ -82,7 +82,7 @@ public class XmlExpectationsHelper { public void assertXmlEqual(String expected, String actual) throws Exception { XmlUnitDiff diff = new XmlUnitDiff(expected, actual); if (diff.hasDifferences()) { - AssertionErrors.fail("Body content " + diff.toString()); + AssertionErrors.fail("Body content " + diff); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/configuration/interfaces/BootstrapWithTestInterface.java b/spring-test/src/test/java/org/springframework/test/context/configuration/interfaces/BootstrapWithTestInterface.java index f8b478d753..188c662b69 100644 --- a/spring-test/src/test/java/org/springframework/test/context/configuration/interfaces/BootstrapWithTestInterface.java +++ b/spring-test/src/test/java/org/springframework/test/context/configuration/interfaces/BootstrapWithTestInterface.java @@ -34,7 +34,7 @@ import static java.util.Collections.singletonList; @BootstrapWith(CustomTestContextBootstrapper.class) interface BootstrapWithTestInterface { - static class CustomTestContextBootstrapper extends DefaultTestContextBootstrapper { + class CustomTestContextBootstrapper extends DefaultTestContextBootstrapper { @Override protected List getContextCustomizerFactories() { diff --git a/spring-test/src/test/java/org/springframework/test/context/configuration/interfaces/ContextConfigurationTestInterface.java b/spring-test/src/test/java/org/springframework/test/context/configuration/interfaces/ContextConfigurationTestInterface.java index 8ef86beafa..fb6f9c1257 100644 --- a/spring-test/src/test/java/org/springframework/test/context/configuration/interfaces/ContextConfigurationTestInterface.java +++ b/spring-test/src/test/java/org/springframework/test/context/configuration/interfaces/ContextConfigurationTestInterface.java @@ -28,7 +28,7 @@ import org.springframework.test.context.configuration.interfaces.ContextConfigur @ContextConfiguration(classes = Config.class) interface ContextConfigurationTestInterface { - static class Config { + class Config { @Bean Employee employee() { diff --git a/spring-test/src/test/java/org/springframework/test/context/configuration/interfaces/WebAppConfigurationTestInterface.java b/spring-test/src/test/java/org/springframework/test/context/configuration/interfaces/WebAppConfigurationTestInterface.java index 4dfbd72403..936261cb06 100644 --- a/spring-test/src/test/java/org/springframework/test/context/configuration/interfaces/WebAppConfigurationTestInterface.java +++ b/spring-test/src/test/java/org/springframework/test/context/configuration/interfaces/WebAppConfigurationTestInterface.java @@ -30,7 +30,7 @@ import org.springframework.test.context.web.WebAppConfiguration; interface WebAppConfigurationTestInterface { @Configuration - static class Config { + class Config { /* no user beans required for these tests */ } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/annotation/meta/ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfig.java b/spring-test/src/test/java/org/springframework/test/context/junit4/annotation/meta/ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfig.java index c1ac540199..a7a1fc8bf8 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/annotation/meta/ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfig.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/annotation/meta/ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfig.java @@ -52,7 +52,7 @@ public @interface ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfig { @Configuration @Profile("dev") - static class DevConfig { + class DevConfig { @Bean public String foo() { @@ -62,7 +62,7 @@ public @interface ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfig { @Configuration @Profile("prod") - static class ProductionConfig { + class ProductionConfig { @Bean public String foo() { @@ -72,7 +72,7 @@ public @interface ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfig { @Configuration @Profile("resolver") - static class ResolverConfig { + class ResolverConfig { @Bean public String foo() { @@ -80,7 +80,7 @@ public @interface ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfig { } } - static class CustomResolver implements ActiveProfilesResolver { + class CustomResolver implements ActiveProfilesResolver { @Override public String[] resolve(Class testClass) { diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/annotation/meta/ConfigClassesAndProfilesWithCustomDefaultsMetaConfig.java b/spring-test/src/test/java/org/springframework/test/context/junit4/annotation/meta/ConfigClassesAndProfilesWithCustomDefaultsMetaConfig.java index 342c4cb54b..44684649b2 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/annotation/meta/ConfigClassesAndProfilesWithCustomDefaultsMetaConfig.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/annotation/meta/ConfigClassesAndProfilesWithCustomDefaultsMetaConfig.java @@ -50,7 +50,7 @@ public @interface ConfigClassesAndProfilesWithCustomDefaultsMetaConfig { @Configuration @Profile("dev") - static class DevConfig { + class DevConfig { @Bean public String foo() { @@ -60,7 +60,7 @@ public @interface ConfigClassesAndProfilesWithCustomDefaultsMetaConfig { @Configuration @Profile("prod") - static class ProductionConfig { + class ProductionConfig { @Bean public String foo() { diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/rules/ProgrammaticTxMgmtSpringRuleTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/rules/ProgrammaticTxMgmtSpringRuleTests.java index 5eea2e7270..acaa79970d 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/rules/ProgrammaticTxMgmtSpringRuleTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/rules/ProgrammaticTxMgmtSpringRuleTests.java @@ -97,24 +97,11 @@ public class ProgrammaticTxMgmtSpringRuleTests { void afterTransaction() { String method = this.testName.getMethodName(); switch (method) { - case "commitTxAndStartNewTx": - case "commitTxButDoNotStartNewTx": { - assertUsers("Dogbert"); - break; - } - case "rollbackTxAndStartNewTx": - case "rollbackTxButDoNotStartNewTx": - case "startTxWithExistingTransaction": { - assertUsers("Dilbert"); - break; - } - case "rollbackTxAndStartNewTxWithDefaultCommitSemantics": { - assertUsers("Dilbert", "Dogbert"); - break; - } - default: { - fail("missing 'after transaction' assertion for test method: " + method); - } + case "commitTxAndStartNewTx", "commitTxButDoNotStartNewTx" -> assertUsers("Dogbert"); + case "rollbackTxAndStartNewTx", "rollbackTxButDoNotStartNewTx", "startTxWithExistingTransaction" -> + assertUsers("Dilbert"); + case "rollbackTxAndStartNewTxWithDefaultCommitSemantics" -> assertUsers("Dilbert", "Dogbert"); + default -> fail("missing 'after transaction' assertion for test method: " + method); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/AnnotatedConfigClassesWithoutAtConfigurationTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/AnnotatedConfigClassesWithoutAtConfigurationTests.java index 8420cfd5bc..19bd9e37b4 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/AnnotatedConfigClassesWithoutAtConfigurationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/AnnotatedConfigClassesWithoutAtConfigurationTests.java @@ -94,7 +94,7 @@ public class AnnotatedConfigClassesWithoutAtConfigurationTests { assertThat(lifecycleBean).isNotNull(); assertThat(lifecycleBean.isInitialized()).isTrue(); Set names = new HashSet<>(); - names.add(enigma.toString()); + names.add(enigma); names.add(lifecycleBean.getName()); assertThat(new HashSet<>(Arrays.asList("enigma #1", "enigma #2"))).isEqualTo(names); } diff --git a/spring-test/src/test/java/org/springframework/test/context/support/DelegatingSmartContextLoaderTests.java b/spring-test/src/test/java/org/springframework/test/context/support/DelegatingSmartContextLoaderTests.java index cc33d62a44..f7ee01e4c9 100644 --- a/spring-test/src/test/java/org/springframework/test/context/support/DelegatingSmartContextLoaderTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/support/DelegatingSmartContextLoaderTests.java @@ -219,7 +219,7 @@ class DelegatingSmartContextLoaderTests { @Bean public String foo() { - return new String("foo"); + return "foo"; } } diff --git a/spring-test/src/test/java/org/springframework/test/context/testng/transaction/programmatic/ProgrammaticTxMgmtTestNGTests.java b/spring-test/src/test/java/org/springframework/test/context/testng/transaction/programmatic/ProgrammaticTxMgmtTestNGTests.java index 4a4e4cadbf..e9415a5f9f 100644 --- a/spring-test/src/test/java/org/springframework/test/context/testng/transaction/programmatic/ProgrammaticTxMgmtTestNGTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/testng/transaction/programmatic/ProgrammaticTxMgmtTestNGTests.java @@ -73,24 +73,11 @@ public class ProgrammaticTxMgmtTestNGTests extends AbstractTransactionalTestNGSp @AfterTransaction public void afterTransaction() { switch (this.methodName) { - case "commitTxAndStartNewTx": - case "commitTxButDoNotStartNewTx": { - assertUsers("Dogbert"); - break; - } - case "rollbackTxAndStartNewTx": - case "rollbackTxButDoNotStartNewTx": - case "startTxWithExistingTransaction": { - assertUsers("Dilbert"); - break; - } - case "rollbackTxAndStartNewTxWithDefaultCommitSemantics": { - assertUsers("Dilbert", "Dogbert"); - break; - } - default: { - fail("missing 'after transaction' assertion for test method: " + this.methodName); - } + case "commitTxAndStartNewTx", "commitTxButDoNotStartNewTx" -> assertUsers("Dogbert"); + case "rollbackTxAndStartNewTx", "rollbackTxButDoNotStartNewTx", "startTxWithExistingTransaction" -> + assertUsers("Dilbert"); + case "rollbackTxAndStartNewTxWithDefaultCommitSemantics" -> assertUsers("Dilbert", "Dogbert"); + default -> fail("missing 'after transaction' assertion for test method: " + this.methodName); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/transaction/programmatic/ProgrammaticTxMgmtTests.java b/spring-test/src/test/java/org/springframework/test/context/transaction/programmatic/ProgrammaticTxMgmtTests.java index db72c16ed2..fdf46e573a 100644 --- a/spring-test/src/test/java/org/springframework/test/context/transaction/programmatic/ProgrammaticTxMgmtTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/transaction/programmatic/ProgrammaticTxMgmtTests.java @@ -88,24 +88,11 @@ class ProgrammaticTxMgmtTests { @AfterTransaction void afterTransaction() { switch (this.methodName) { - case "commitTxAndStartNewTx": - case "commitTxButDoNotStartNewTx": { - assertUsers("Dogbert"); - break; - } - case "rollbackTxAndStartNewTx": - case "rollbackTxButDoNotStartNewTx": - case "startTxWithExistingTransaction": { - assertUsers("Dilbert"); - break; - } - case "rollbackTxAndStartNewTxWithDefaultCommitSemantics": { - assertUsers("Dilbert", "Dogbert"); - break; - } - default: { - fail("missing 'after transaction' assertion for test method: " + this.methodName); - } + case "commitTxAndStartNewTx", "commitTxButDoNotStartNewTx" -> assertUsers("Dogbert"); + case "rollbackTxAndStartNewTx", "rollbackTxButDoNotStartNewTx", "startTxWithExistingTransaction" -> + assertUsers("Dilbert"); + case "rollbackTxAndStartNewTxWithDefaultCommitSemantics" -> assertUsers("Dilbert", "Dogbert"); + default -> fail("missing 'after transaction' assertion for test method: " + this.methodName); } } diff --git a/spring-test/src/test/java/org/springframework/test/util/ReflectionTestUtilsTests.java b/spring-test/src/test/java/org/springframework/test/util/ReflectionTestUtilsTests.java index 680ed0bf37..fdea42df99 100644 --- a/spring-test/src/test/java/org/springframework/test/util/ReflectionTestUtilsTests.java +++ b/spring-test/src/test/java/org/springframework/test/util/ReflectionTestUtilsTests.java @@ -46,7 +46,7 @@ import static org.springframework.test.util.ReflectionTestUtils.setField; */ class ReflectionTestUtilsTests { - private static final Float PI = Float.valueOf((float) 22 / 7); + private static final Float PI = (float) 22 / 7; private final Person person = new PersonEntity(); @@ -64,7 +64,7 @@ class ReflectionTestUtilsTests { @Test void setFieldWithNullTargetObject() throws Exception { assertThatIllegalArgumentException() - .isThrownBy(() -> setField((Object) null, "id", Long.valueOf(99))) + .isThrownBy(() -> setField((Object) null, "id", 99L)) .withMessageStartingWith("Either targetObject or targetClass"); } @@ -78,7 +78,7 @@ class ReflectionTestUtilsTests { @Test void setFieldWithNullTargetClass() throws Exception { assertThatIllegalArgumentException() - .isThrownBy(() -> setField((Class) null, "id", Long.valueOf(99))) + .isThrownBy(() -> setField((Class) null, "id", 99L)) .withMessageStartingWith("Either targetObject or targetClass"); } @@ -92,21 +92,21 @@ class ReflectionTestUtilsTests { @Test void setFieldWithNullNameAndNullType() throws Exception { assertThatIllegalArgumentException() - .isThrownBy(() -> setField(person, null, Long.valueOf(99), null)) + .isThrownBy(() -> setField(person, null, 99L, null)) .withMessageStartingWith("Either name or type"); } @Test void setFieldWithBogusName() throws Exception { assertThatIllegalArgumentException() - .isThrownBy(() -> setField(person, "bogus", Long.valueOf(99), long.class)) + .isThrownBy(() -> setField(person, "bogus", 99L, long.class)) .withMessageStartingWith("Could not find field 'bogus'"); } @Test void setFieldWithWrongType() throws Exception { assertThatIllegalArgumentException() - .isThrownBy(() -> setField(person, "id", Long.valueOf(99), String.class)) + .isThrownBy(() -> setField(person, "id", 99L, String.class)) .withMessageStartingWith("Could not find field"); } @@ -135,17 +135,17 @@ class ReflectionTestUtilsTests { private static void assertSetFieldAndGetFieldBehavior(Person person) { // Set reflectively - setField(person, "id", Long.valueOf(99), long.class); + setField(person, "id", 99L, long.class); setField(person, "name", "Tom"); - setField(person, "age", Integer.valueOf(42)); + setField(person, "age", 42); setField(person, "eyeColor", "blue", String.class); setField(person, "likesPets", Boolean.TRUE); setField(person, "favoriteNumber", PI, Number.class); // Get reflectively - assertThat(getField(person, "id")).isEqualTo(Long.valueOf(99)); + assertThat(getField(person, "id")).isEqualTo(99L); assertThat(getField(person, "name")).isEqualTo("Tom"); - assertThat(getField(person, "age")).isEqualTo(Integer.valueOf(42)); + assertThat(getField(person, "age")).isEqualTo(42); assertThat(getField(person, "eyeColor")).isEqualTo("blue"); assertThat(getField(person, "likesPets")).isEqualTo(Boolean.TRUE); assertThat(getField(person, "favoriteNumber")).isEqualTo(PI); @@ -249,33 +249,33 @@ class ReflectionTestUtilsTests { @Test void invokeSetterMethodAndInvokeGetterMethodWithExplicitMethodNames() throws Exception { - invokeSetterMethod(person, "setId", Long.valueOf(1), long.class); + invokeSetterMethod(person, "setId", 1L, long.class); invokeSetterMethod(person, "setName", "Jerry", String.class); - invokeSetterMethod(person, "setAge", Integer.valueOf(33), int.class); + invokeSetterMethod(person, "setAge", 33, int.class); invokeSetterMethod(person, "setEyeColor", "green", String.class); invokeSetterMethod(person, "setLikesPets", Boolean.FALSE, boolean.class); - invokeSetterMethod(person, "setFavoriteNumber", Integer.valueOf(42), Number.class); + invokeSetterMethod(person, "setFavoriteNumber", 42, Number.class); assertThat(person.getId()).as("ID (protected method in a superclass)").isEqualTo(1); assertThat(person.getName()).as("name (private method)").isEqualTo("Jerry"); assertThat(person.getAge()).as("age (protected method)").isEqualTo(33); assertThat(person.getEyeColor()).as("eye color (package private method)").isEqualTo("green"); assertThat(person.likesPets()).as("'likes pets' flag (protected method for a boolean)").isFalse(); - assertThat(person.getFavoriteNumber()).as("'favorite number' (protected method for a Number)").isEqualTo(Integer.valueOf(42)); + assertThat(person.getFavoriteNumber()).as("'favorite number' (protected method for a Number)").isEqualTo(42); - assertThat(invokeGetterMethod(person, "getId")).isEqualTo(Long.valueOf(1)); + assertThat(invokeGetterMethod(person, "getId")).isEqualTo(1L); assertThat(invokeGetterMethod(person, "getName")).isEqualTo("Jerry"); - assertThat(invokeGetterMethod(person, "getAge")).isEqualTo(Integer.valueOf(33)); + assertThat(invokeGetterMethod(person, "getAge")).isEqualTo(33); assertThat(invokeGetterMethod(person, "getEyeColor")).isEqualTo("green"); assertThat(invokeGetterMethod(person, "likesPets")).isEqualTo(Boolean.FALSE); - assertThat(invokeGetterMethod(person, "getFavoriteNumber")).isEqualTo(Integer.valueOf(42)); + assertThat(invokeGetterMethod(person, "getFavoriteNumber")).isEqualTo(42); } @Test void invokeSetterMethodAndInvokeGetterMethodWithJavaBeanPropertyNames() throws Exception { - invokeSetterMethod(person, "id", Long.valueOf(99), long.class); + invokeSetterMethod(person, "id", 99L, long.class); invokeSetterMethod(person, "name", "Tom"); - invokeSetterMethod(person, "age", Integer.valueOf(42)); + invokeSetterMethod(person, "age", 42); invokeSetterMethod(person, "eyeColor", "blue", String.class); invokeSetterMethod(person, "likesPets", Boolean.TRUE); invokeSetterMethod(person, "favoriteNumber", PI, Number.class); @@ -287,9 +287,9 @@ class ReflectionTestUtilsTests { assertThat(person.likesPets()).as("'likes pets' flag (protected method for a boolean)").isTrue(); assertThat(person.getFavoriteNumber()).as("'favorite number' (protected method for a Number)").isEqualTo(PI); - assertThat(invokeGetterMethod(person, "id")).isEqualTo(Long.valueOf(99)); + assertThat(invokeGetterMethod(person, "id")).isEqualTo(99L); assertThat(invokeGetterMethod(person, "name")).isEqualTo("Tom"); - assertThat(invokeGetterMethod(person, "age")).isEqualTo(Integer.valueOf(42)); + assertThat(invokeGetterMethod(person, "age")).isEqualTo(42); assertThat(invokeGetterMethod(person, "eyeColor")).isEqualTo("blue"); assertThat(invokeGetterMethod(person, "likesPets")).isEqualTo(Boolean.TRUE); assertThat(invokeGetterMethod(person, "favoriteNumber")).isEqualTo(PI); @@ -349,7 +349,7 @@ class ReflectionTestUtilsTests { assertThat(component.getText()).as("text").isNull(); // Simulate autowiring a configuration method - invokeMethod(component, "configure", Integer.valueOf(42), "enigma"); + invokeMethod(component, "configure", 42, "enigma"); assertThat(component.getNumber()).as("number should have been configured").isEqualTo(Integer.valueOf(42)); assertThat(component.getText()).as("text should have been configured").isEqualTo("enigma"); @@ -380,14 +380,14 @@ class ReflectionTestUtilsTests { @Test void invokeMethodWithTooFewArguments() { assertThatIllegalStateException() - .isThrownBy(() -> invokeMethod(component, "configure", Integer.valueOf(42))) + .isThrownBy(() -> invokeMethod(component, "configure", 42)) .withMessageStartingWith("Method not found"); } @Test void invokeMethodWithTooManyArguments() { assertThatIllegalStateException() - .isThrownBy(() -> invokeMethod(component, "configure", Integer.valueOf(42), "enigma", "baz", "quux")) + .isThrownBy(() -> invokeMethod(component, "configure", 42, "enigma", "baz", "quux")) .withMessageStartingWith("Method not found"); } @@ -406,7 +406,7 @@ class ReflectionTestUtilsTests { @Test // SPR-14363 void invokeMethodOnLegacyEntityWithSideEffectsInToString() { - invokeMethod(entity, "configure", Integer.valueOf(42), "enigma"); + invokeMethod(entity, "configure", 42, "enigma"); assertThat(entity.getNumber()).as("number should have been configured").isEqualTo(Integer.valueOf(42)); assertThat(entity.getText()).as("text should have been configured").isEqualTo("enigma"); } diff --git a/spring-test/src/test/java/org/springframework/test/web/client/MockRestServiceServerTests.java b/spring-test/src/test/java/org/springframework/test/web/client/MockRestServiceServerTests.java index a9974bddad..f0eb28818a 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/MockRestServiceServerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/MockRestServiceServerTests.java @@ -183,9 +183,11 @@ public class MockRestServiceServerTests { this.restTemplate.getForObject("/foo", Void.class); assertThatThrownBy(() -> server1.verify(Duration.ofMillis(100))).hasMessage( - "Further request(s) expected leaving 1 unsatisfied expectation(s).\n" + - "1 request(s) executed:\n" + - "GET /foo, headers: [Accept:\"application/json, application/*+json\"]\n"); + """ + Further request(s) expected leaving 1 unsatisfied expectation(s). + 1 request(s) executed: + GET /foo, headers: [Accept:"application/json, application/*+json"] + """); MockRestServiceServer server2 = builder.build(); server2.expect(requestTo("/foo")).andRespond(withSuccess()); diff --git a/spring-test/src/test/java/org/springframework/test/web/client/SimpleRequestExpectationManagerTests.java b/spring-test/src/test/java/org/springframework/test/web/client/SimpleRequestExpectationManagerTests.java index 33efa8d17f..7479cc7fa8 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/SimpleRequestExpectationManagerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/SimpleRequestExpectationManagerTests.java @@ -55,8 +55,10 @@ public class SimpleRequestExpectationManagerTests { this.manager.validateRequest(createRequest(GET, "/foo")); } catch (AssertionError error) { - assertThat(error.getMessage()).isEqualTo(("No further requests expected: HTTP GET /foo\n" + - "0 request(s) executed.\n")); + assertThat(error.getMessage()).isEqualTo((""" + No further requests expected: HTTP GET /foo + 0 request(s) executed. + """)); } } @@ -83,10 +85,12 @@ public class SimpleRequestExpectationManagerTests { this.manager.validateRequest(createRequest(GET, "/bar")); assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> this.manager.validateRequest(createRequest(GET, "/baz"))) - .withMessage("No further requests expected: HTTP GET /baz\n" + - "2 request(s) executed:\n" + - "GET /foo\n" + - "GET /bar\n"); + .withMessage(""" + No further requests expected: HTTP GET /baz + 2 request(s) executed: + GET /foo + GET /bar + """); } @Test @@ -96,8 +100,11 @@ public class SimpleRequestExpectationManagerTests { this.manager.validateRequest(createRequest(GET, "/foo")); assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> this.manager.verify()) - .withMessage("Further request(s) expected leaving 1 unsatisfied expectation(s).\n" + - "1 request(s) executed:\nGET /foo\n"); + .withMessage(""" + Further request(s) expected leaving 1 unsatisfied expectation(s). + 1 request(s) executed: + GET /foo + """); } @Test @@ -124,12 +131,14 @@ public class SimpleRequestExpectationManagerTests { this.manager.validateRequest(createRequest(GET, "/bar")); assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> this.manager.validateRequest(createRequest(GET, "/foo"))) - .withMessage("No further requests expected: HTTP GET /foo\n" + - "4 request(s) executed:\n" + - "GET /foo\n" + - "GET /bar\n" + - "GET /foo\n" + - "GET /bar\n"); + .withMessage(""" + No further requests expected: HTTP GET /foo + 4 request(s) executed: + GET /foo + GET /bar + GET /foo + GET /bar + """); } @Test @@ -141,10 +150,12 @@ public class SimpleRequestExpectationManagerTests { this.manager.validateRequest(createRequest(GET, "/foo")); assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> this.manager.verify()) - .withMessageContaining("3 request(s) executed:\n" + - "GET /foo\n" + - "GET /bar\n" + - "GET /foo\n"); + .withMessageContaining(""" + 3 request(s) executed: + GET /foo + GET /bar + GET /foo + """); } @Test diff --git a/spring-test/src/test/java/org/springframework/test/web/client/UnorderedRequestExpectationManagerTests.java b/spring-test/src/test/java/org/springframework/test/web/client/UnorderedRequestExpectationManagerTests.java index 973a44df58..dc9f9c754b 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/UnorderedRequestExpectationManagerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/UnorderedRequestExpectationManagerTests.java @@ -52,8 +52,10 @@ public class UnorderedRequestExpectationManagerTests { this.manager.validateRequest(createRequest(GET, "/foo")); } catch (AssertionError error) { - assertThat(error.getMessage()).isEqualTo(("No further requests expected: HTTP GET /foo\n" + - "0 request(s) executed.\n")); + assertThat(error.getMessage()).isEqualTo((""" + No further requests expected: HTTP GET /foo + 0 request(s) executed. + """)); } } @@ -94,12 +96,14 @@ public class UnorderedRequestExpectationManagerTests { this.manager.validateRequest(createRequest(GET, "/foo")); assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> this.manager.validateRequest(createRequest(GET, "/foo"))) - .withMessage("No further requests expected: HTTP GET /foo\n" + - "4 request(s) executed:\n" + - "GET /bar\n" + - "GET /foo\n" + - "GET /bar\n" + - "GET /foo\n"); + .withMessage(""" + No further requests expected: HTTP GET /foo + 4 request(s) executed: + GET /bar + GET /foo + GET /bar + GET /foo + """); } @Test @@ -111,10 +115,12 @@ public class UnorderedRequestExpectationManagerTests { this.manager.validateRequest(createRequest(GET, "/foo")); assertThatExceptionOfType(AssertionError.class) .isThrownBy(this.manager::verify) - .withMessageContaining("3 request(s) executed:\n" + - "GET /bar\n" + - "GET /foo\n" + - "GET /foo\n"); + .withMessageContaining(""" + 3 request(s) executed: + GET /bar + GET /foo + GET /foo + """); } diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/SoftAssertionTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/SoftAssertionTests.java index db7b242fff..58b2160631 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/SoftAssertionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/SoftAssertionTests.java @@ -56,9 +56,10 @@ class SoftAssertionTests { responseSpec -> responseSpec.expectBody(String.class).isEqualTo("bogus") ) ) - .withMessage("Multiple Exceptions (2):\n" + - "Status expected:<400 BAD_REQUEST> but was:<200 OK>\n" + - "Response body expected: but was:"); + .withMessage(""" + Multiple Exceptions (2): + Status expected:<400 BAD_REQUEST> but was:<200 OK> + Response body expected: but was:"""); } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnectionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnectionTests.java index 05506cfbe3..0c5e42e3ca 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnectionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnectionTests.java @@ -17,6 +17,7 @@ package org.springframework.test.web.servlet.htmlunit; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.Collections; import com.gargoylesoftware.htmlunit.HttpWebConnection; @@ -79,7 +80,7 @@ public class DelegatingWebConnectionTests { @BeforeEach public void setup() throws Exception { request = new WebRequest(new URL("http://localhost/")); - WebResponseData data = new WebResponseData("".getBytes("UTF-8"), 200, "", Collections.emptyList()); + WebResponseData data = new WebResponseData("".getBytes(StandardCharsets.UTF_8), 200, "", Collections.emptyList()); expectedResponse = new WebResponse(data, request, 100L); webConnection = new DelegatingWebConnection(defaultConnection, new DelegateWebConnection(matcher1, connection1), new DelegateWebConnection(matcher2, connection2)); diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java index 2f5a0f8ece..3342a78766 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java @@ -414,7 +414,7 @@ class MockHttpServletRequestBuilderTests { @Test void body() throws IOException { - byte[] body = "Hello World".getBytes("UTF-8"); + byte[] body = "Hello World".getBytes(StandardCharsets.UTF_8); this.builder.content(body); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/result/ContentResultMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/result/ContentResultMatchersTests.java index 6884bdf15f..cb10e6579d 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/result/ContentResultMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/result/ContentResultMatchersTests.java @@ -46,7 +46,7 @@ public class ContentResultMatchersTests { @Test public void string() throws Exception { - new ContentResultMatchers().string(new String(CONTENT.getBytes("UTF-8"))).match(getStubMvcResult(CONTENT)); + new ContentResultMatchers().string(new String(CONTENT.getBytes(StandardCharsets.UTF_8))).match(getStubMvcResult(CONTENT)); } @Test @@ -57,7 +57,7 @@ public class ContentResultMatchersTests { @Test public void stringMatcher() throws Exception { - String content = new String(CONTENT.getBytes("UTF-8")); + String content = new String(CONTENT.getBytes(StandardCharsets.UTF_8)); new ContentResultMatchers().string(Matchers.equalTo(content)).match(getStubMvcResult(CONTENT)); } @@ -69,7 +69,7 @@ public class ContentResultMatchersTests { @Test public void bytes() throws Exception { - new ContentResultMatchers().bytes(CONTENT.getBytes("UTF-8")).match(getStubMvcResult(CONTENT)); + new ContentResultMatchers().bytes(CONTENT.getBytes(StandardCharsets.UTF_8)).match(getStubMvcResult(CONTENT)); } @Test diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java index 03767591f6..541e699eb3 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java @@ -302,7 +302,7 @@ public class JsonPathResultMatchersTests { public void prefixWithPayloadNotLongEnough() throws Exception { MockHttpServletResponse response = new MockHttpServletResponse(); response.addHeader("Content-Type", "application/json"); - response.getWriter().print(new String("test".getBytes("ISO-8859-1"))); + response.getWriter().print(new String("test".getBytes(StandardCharsets.ISO_8859_1))); StubMvcResult result = new StubMvcResult(null, null, null, null, null, null, response); assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> @@ -312,7 +312,7 @@ public class JsonPathResultMatchersTests { private StubMvcResult createPrefixedStubMvcResult(String jsonPrefix) throws Exception { MockHttpServletResponse response = new MockHttpServletResponse(); response.addHeader("Content-Type", "application/json"); - response.getWriter().print(jsonPrefix + new String(RESPONSE_CONTENT.getBytes("ISO-8859-1"))); + response.getWriter().print(jsonPrefix + new String(RESPONSE_CONTENT.getBytes(StandardCharsets.ISO_8859_1))); return new StubMvcResult(null, null, null, null, null, null, response); } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/result/PrintingResultHandlerTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/result/PrintingResultHandlerTests.java index 7860292b6a..383eda23f6 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/result/PrintingResultHandlerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/result/PrintingResultHandlerTests.java @@ -17,6 +17,7 @@ package org.springframework.test.web.servlet.result; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -74,7 +75,7 @@ public class PrintingResultHandlerTests { this.request.addHeader("header", "headerValue"); this.request.setCharacterEncoding("UTF-16"); String palindrome = "ablE was I ere I saw Elba"; - byte[] bytes = palindrome.getBytes("UTF-16"); + byte[] bytes = palindrome.getBytes(StandardCharsets.UTF_16); this.request.setContent(bytes); this.request.getSession().setAttribute("foo", "bar"); @@ -100,7 +101,7 @@ public class PrintingResultHandlerTests { this.request.addHeader("header", "headerValue"); this.request.setCharacterEncoding("UTF-16"); String palindrome = "ablE was I ere I saw Elba"; - byte[] bytes = palindrome.getBytes("UTF-16"); + byte[] bytes = palindrome.getBytes(StandardCharsets.UTF_16); this.request.setContent(bytes); this.handler.handle(this.mvcResult); @@ -124,7 +125,7 @@ public class PrintingResultHandlerTests { this.request.addHeader("header", "headerValue"); this.request.setCharacterEncoding("UTF-16"); String palindrome = "ablE was I ere I saw Elba"; - byte[] bytes = palindrome.getBytes("UTF-16"); + byte[] bytes = palindrome.getBytes(StandardCharsets.UTF_16); this.request.setContent(bytes); this.request.setSession(Mockito.mock(HttpSession.class)); @@ -204,7 +205,7 @@ public class PrintingResultHandlerTests { @Test public void printRequestWithCharacterEncoding() throws Exception { this.request.setCharacterEncoding("UTF-8"); - this.request.setContent("text".getBytes("UTF-8")); + this.request.setContent("text".getBytes(StandardCharsets.UTF_8)); this.handler.handle(this.mvcResult); diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/result/StatusResultMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/result/StatusResultMatchersTests.java index c3a82c1502..e893c2e149 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/result/StatusResultMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/result/StatusResultMatchersTests.java @@ -85,23 +85,12 @@ public class StatusResultMatchersTests { response.setStatus(status.value()); MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response); switch (status.series().value()) { - case 1: - this.matchers.is1xxInformational().match(mvcResult); - break; - case 2: - this.matchers.is2xxSuccessful().match(mvcResult); - break; - case 3: - this.matchers.is3xxRedirection().match(mvcResult); - break; - case 4: - this.matchers.is4xxClientError().match(mvcResult); - break; - case 5: - this.matchers.is5xxServerError().match(mvcResult); - break; - default: - fail("Unexpected range for status code value " + status); + case 1 -> this.matchers.is1xxInformational().match(mvcResult); + case 2 -> this.matchers.is2xxSuccessful().match(mvcResult); + case 3 -> this.matchers.is3xxRedirection().match(mvcResult); + case 4 -> this.matchers.is4xxClientError().match(mvcResult); + case 5 -> this.matchers.is5xxServerError().match(mvcResult); + default -> fail("Unexpected range for status code value " + status); } } } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/ExceptionHandlerTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/ExceptionHandlerTests.java index 75c8041464..fb71ea0261 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/ExceptionHandlerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/ExceptionHandlerTests.java @@ -173,14 +173,11 @@ class ExceptionHandlerTests { @GetMapping("/person/{name}") Person get(@PathVariable String name) { - switch (name) { - case "Luke": - throw new IllegalArgumentException(); - case "Leia": - throw new IllegalStateException(); - default: - return new Person("Yoda"); - } + return switch (name) { + case "Luke" -> throw new IllegalArgumentException(); + case "Leia" -> throw new IllegalStateException(); + default -> new Person("Yoda"); + }; } @ExceptionHandler diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/resultmatches/XpathAssertionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/resultmatches/XpathAssertionTests.java index e7f50190af..c7e2964ecb 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/resultmatches/XpathAssertionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/resultmatches/XpathAssertionTests.java @@ -224,11 +224,12 @@ public class XpathAssertionTests { @RequestMapping(value = "/blog.atom", method = {GET, HEAD}) @ResponseBody public String listPublishedPosts() { - return "\r\n" - + "\r\n" - + " Test Feed\r\n" - + " https://www.example.com/favicon.ico\r\n" - + "\r\n\r\n"; + return """ + + + Test Feed + https://www.example.com/favicon.ico + """.replaceAll("\n", "\r\n"); } } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ExceptionHandlerTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ExceptionHandlerTests.java index d6b026bed5..bf0b9b7c2a 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ExceptionHandlerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ExceptionHandlerTests.java @@ -152,14 +152,11 @@ public class ExceptionHandlerTests { @GetMapping("/person/{name}") Person get(@PathVariable String name) { - switch (name) { - case "Luke": - throw new IllegalArgumentException(); - case "Leia": - throw new IllegalStateException(); - default: - return new Person("Yoda"); - } + return switch (name) { + case "Luke" -> throw new IllegalArgumentException(); + case "Leia" -> throw new IllegalStateException(); + default -> new Person("Yoda"); + }; } @ExceptionHandler diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/MultipartControllerTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/MultipartControllerTests.java index 2d57e1ab64..4a9f6c678f 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/MultipartControllerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/MultipartControllerTests.java @@ -70,18 +70,12 @@ class MultipartControllerTests { byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8); MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json); - MockMultipartHttpServletRequestBuilder requestBuilder; - switch (url) { - case "/multipartfile": - requestBuilder = multipart(url).file(new MockMultipartFile("file", "orig", null, fileContent)); - break; - case "/multipartfile-via-put": - requestBuilder = multipart(HttpMethod.PUT, url).file(new MockMultipartFile("file", "orig", null, fileContent)); - break; - default: - requestBuilder = multipart(url).part(new MockPart("part", "orig", fileContent)); - break; - } + MockMultipartHttpServletRequestBuilder requestBuilder = switch (url) { + case "/multipartfile" -> multipart(url).file(new MockMultipartFile("file", "orig", null, fileContent)); + case "/multipartfile-via-put" -> + multipart(HttpMethod.PUT, url).file(new MockMultipartFile("file", "orig", null, fileContent)); + default -> multipart(url).part(new MockPart("part", "orig", fileContent)); + }; standaloneSetup(new MultipartController()).build() .perform(requestBuilder.file(jsonPart)) diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resulthandlers/PrintingResultHandlerSmokeTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resulthandlers/PrintingResultHandlerSmokeTests.java index 87986b2a01..0af5ea28dd 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resulthandlers/PrintingResultHandlerSmokeTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resulthandlers/PrintingResultHandlerSmokeTests.java @@ -68,7 +68,7 @@ public class PrintingResultHandlerSmokeTests { System.out.println(); System.out.println("==============================================================="); - System.out.println(writer.toString()); + System.out.println(writer); } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java index 2efc51e4af..b4a47c3710 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java @@ -79,10 +79,10 @@ public class ContentAssertionTests { @Test void contentAsBytes() throws Exception { this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)) - .andExpect(content().bytes("Hello world!".getBytes("ISO-8859-1"))); + .andExpect(content().bytes("Hello world!".getBytes(StandardCharsets.ISO_8859_1))); this.mockMvc.perform(get("/handleUtf8")) - .andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8"))); + .andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes(StandardCharsets.UTF_8))); } @Test @@ -103,11 +103,11 @@ public class ContentAssertionTests { this.mockMvc.perform(get("/handleUtf8")) .andExpect(content().encoding("UTF-8")) - .andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8"))); + .andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes(StandardCharsets.UTF_8))); this.mockMvc.perform(get("/handleUtf8")) .andExpect(content().encoding(StandardCharsets.UTF_8)) - .andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8"))); + .andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes(StandardCharsets.UTF_8))); } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/XpathAssertionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/XpathAssertionTests.java index 3bece04be7..a6c35af8e0 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/XpathAssertionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/XpathAssertionTests.java @@ -228,11 +228,12 @@ public class XpathAssertionTests { @RequestMapping(value="/blog.atom", method = { GET, HEAD }) @ResponseBody public String listPublishedPosts() { - return "\r\n" - + "\r\n" - + " Test Feed\r\n" - + " https://www.example.com/favicon.ico\r\n" - + "\r\n\r\n"; + return """ + + + Test Feed + https://www.example.com/favicon.ico + """.replaceAll("\n", "\r\n"); } } diff --git a/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt index 72d750e413..d1e731f3bc 100644 --- a/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt +++ b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt @@ -16,21 +16,27 @@ package org.springframework.test.web.servlet -import org.assertj.core.api.Assertions.* +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatExceptionOfType import org.hamcrest.CoreMatchers -import org.hamcrest.Matcher -import org.hamcrest.Matchers import org.junit.jupiter.api.Test import org.springframework.http.HttpMethod import org.springframework.http.HttpStatus -import org.springframework.http.MediaType.* +import org.springframework.http.MediaType.APPLICATION_ATOM_XML +import org.springframework.http.MediaType.APPLICATION_JSON +import org.springframework.http.MediaType.APPLICATION_XML import org.springframework.test.web.Person import org.springframework.test.web.servlet.setup.MockMvcBuilders -import org.springframework.web.bind.annotation.* +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.ResponseStatus +import org.springframework.web.bind.annotation.RestController import org.springframework.web.servlet.ModelAndView import reactor.core.publisher.Mono import java.security.Principal -import java.util.* +import java.util.Locale /** * [MockMvc] DSL tests that verifies builder, actions and expect blocks.