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
This commit is contained in:
committed by
Sam Brannen
parent
307247b6a3
commit
debe78b7f9
@@ -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<ContextCustomizerFactory> getContextCustomizerFactories() {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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 */
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -94,7 +94,7 @@ public class AnnotatedConfigClassesWithoutAtConfigurationTests {
|
||||
assertThat(lifecycleBean).isNotNull();
|
||||
assertThat(lifecycleBean.isInitialized()).isTrue();
|
||||
Set<String> 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);
|
||||
}
|
||||
|
||||
@@ -219,7 +219,7 @@ class DelegatingSmartContextLoaderTests {
|
||||
|
||||
@Bean
|
||||
public String foo() {
|
||||
return new String("foo");
|
||||
return "foo";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
""");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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:<bogus> but was:<hello>");
|
||||
.withMessage("""
|
||||
Multiple Exceptions (2):
|
||||
Status expected:<400 BAD_REQUEST> but was:<200 OK>
|
||||
Response body expected:<bogus> but was:<hello>""");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -224,11 +224,12 @@ public class XpathAssertionTests {
|
||||
@RequestMapping(value = "/blog.atom", method = {GET, HEAD})
|
||||
@ResponseBody
|
||||
public String listPublishedPosts() {
|
||||
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
|
||||
+ "<feed xmlns=\"http://www.w3.org/2005/Atom\">\r\n"
|
||||
+ " <title>Test Feed</title>\r\n"
|
||||
+ " <icon>https://www.example.com/favicon.ico</icon>\r\n"
|
||||
+ "</feed>\r\n\r\n";
|
||||
return """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<feed xmlns="http://www.w3.org/2005/Atom">
|
||||
<title>Test Feed</title>
|
||||
<icon>https://www.example.com/favicon.ico</icon>
|
||||
</feed>""".replaceAll("\n", "\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -68,7 +68,7 @@ public class PrintingResultHandlerSmokeTests {
|
||||
|
||||
System.out.println();
|
||||
System.out.println("===============================================================");
|
||||
System.out.println(writer.toString());
|
||||
System.out.println(writer);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -228,11 +228,12 @@ public class XpathAssertionTests {
|
||||
@RequestMapping(value="/blog.atom", method = { GET, HEAD })
|
||||
@ResponseBody
|
||||
public String listPublishedPosts() {
|
||||
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
|
||||
+ "<feed xmlns=\"http://www.w3.org/2005/Atom\">\r\n"
|
||||
+ " <title>Test Feed</title>\r\n"
|
||||
+ " <icon>https://www.example.com/favicon.ico</icon>\r\n"
|
||||
+ "</feed>\r\n\r\n";
|
||||
return """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<feed xmlns="http://www.w3.org/2005/Atom">
|
||||
<title>Test Feed</title>
|
||||
<icon>https://www.example.com/favicon.ico</icon>
|
||||
</feed>""".replaceAll("\n", "\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user