diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java index 41cbc594fa..681216d1ca 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java @@ -15,8 +15,7 @@ */ package org.springframework.boot.actuate.autoconfigure.web.server; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.util.function.Consumer; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -30,6 +29,7 @@ import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; +import org.springframework.util.StringUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -51,16 +51,14 @@ class ManagementContextAutoConfigurationTests { ServletManagementContextAutoConfiguration.class, WebEndpointAutoConfiguration.class, EndpointAutoConfiguration.class)); contextRunner.withPropertyValues("server.port=0", "management.server.port=0") - .run((context) -> assertThat(tomcatStartedOccurencesIn(output)).isEqualTo(2)); + .run((context) -> assertThat(output).satisfies(numberOfOccurrences("Tomcat started on port", 2))); } - private int tomcatStartedOccurencesIn(CharSequence output) { - int matches = 0; - Matcher matcher = Pattern.compile("Tomcat started on port").matcher(output); - while (matcher.find()) { - matches++; - } - return matches; + private Consumer numberOfOccurrences(String substring, int expectedCount) { + return (charSequence) -> { + int count = StringUtils.countOccurrencesOf(charSequence.toString(), substring); + assertThat(count).isEqualTo(expectedCount); + }; } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityFilterAutoConfigurationEarlyInitializationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityFilterAutoConfigurationEarlyInitializationTests.java index 0a04e3f74d..c186f9c009 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityFilterAutoConfigurationEarlyInitializationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityFilterAutoConfigurationEarlyInitializationTests.java @@ -16,6 +16,9 @@ package org.springframework.boot.autoconfigure.security.servlet; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; @@ -45,6 +48,8 @@ import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import static org.assertj.core.api.Assertions.assertThat; + /** * Integration test to ensure {@link SecurityFilterAutoConfiguration} doesn't cause early * initialization. @@ -54,6 +59,9 @@ import org.springframework.web.bind.annotation.RestController; @ExtendWith(OutputCaptureExtension.class) class SecurityFilterAutoConfigurationEarlyInitializationTests { + private static final Pattern PASSWORD_PATTERN = Pattern.compile("^Using generated security password: (.*)$", + Pattern.MULTILINE); + @Test void testSecurityFilterDoesNotCauseEarlyInitialization(CapturedOutput output) { try (AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext()) { @@ -61,8 +69,9 @@ class SecurityFilterAutoConfigurationEarlyInitializationTests { context.register(Config.class); context.refresh(); int port = context.getWebServer().getPort(); - String password = output.toString().split("Using generated security password: ")[1].split("\n")[0].trim(); - new TestRestTemplate("user", password).getForEntity("http://localhost:" + port, Object.class); + Matcher password = PASSWORD_PATTERN.matcher(output); + assertThat(password.find()).isTrue(); + new TestRestTemplate("user", password.group(1)).getForEntity("http://localhost:" + port, Object.class); // If early initialization occurred a ConverterNotFoundException is thrown } } diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/json/DuplicateJsonObjectContextCustomizerFactoryTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/json/DuplicateJsonObjectContextCustomizerFactoryTests.java index 4580c9f036..4d330e96f4 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/json/DuplicateJsonObjectContextCustomizerFactoryTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/json/DuplicateJsonObjectContextCustomizerFactoryTests.java @@ -42,8 +42,7 @@ public class DuplicateJsonObjectContextCustomizerFactoryTests { public void warningForMultipleVersions() { new DuplicateJsonObjectContextCustomizerFactory().createContextCustomizer(null, null).customizeContext(null, null); - assertThat(this.output.toString()) - .contains("Found multiple occurrences of org.json.JSONObject on the class path:"); + assertThat(this.output).contains("Found multiple occurrences of org.json.JSONObject on the class path:"); } } diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-atomikos/src/test/java/smoketest/atomikos/SampleAtomikosApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-atomikos/src/test/java/smoketest/atomikos/SampleAtomikosApplicationTests.java index 55cd39bf6f..33eb82c079 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-atomikos/src/test/java/smoketest/atomikos/SampleAtomikosApplicationTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-atomikos/src/test/java/smoketest/atomikos/SampleAtomikosApplicationTests.java @@ -16,12 +16,14 @@ package smoketest.atomikos; -import org.assertj.core.api.Condition; +import java.util.function.Consumer; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; +import org.springframework.util.StringUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -36,24 +38,16 @@ class SampleAtomikosApplicationTests { @Test void testTransactionRollback(CapturedOutput output) throws Exception { SampleAtomikosApplication.main(new String[] {}); - assertThat(output.toString()).has(substring(1, "---->")).has(substring(1, "----> josh")) - .has(substring(2, "Count is 1")).has(substring(1, "Simulated error")); + assertThat(output).satisfies(numberOfOccurrences("---->", 1)); + assertThat(output).satisfies(numberOfOccurrences("----> josh", 1)); + assertThat(output).satisfies(numberOfOccurrences("Count is 1", 2)); + assertThat(output).satisfies(numberOfOccurrences("Simulated error", 1)); } - private Condition substring(int times, String substring) { - return new Condition("containing '" + substring + "' " + times + " times") { - - @Override - public boolean matches(String value) { - int i = 0; - while (value.contains(substring)) { - int beginIndex = value.indexOf(substring) + substring.length(); - value = value.substring(beginIndex); - i++; - } - return i == times; - } - + private Consumer numberOfOccurrences(String substring, int expectedCount) { + return (charSequence) -> { + int count = StringUtils.countOccurrencesOf(charSequence.toString(), substring); + assertThat(count).isEqualTo(expectedCount); }; } diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-bitronix/src/test/java/smoketest/bitronix/SampleBitronixApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-bitronix/src/test/java/smoketest/bitronix/SampleBitronixApplicationTests.java index 3b77cdf30d..422980c4a0 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-bitronix/src/test/java/smoketest/bitronix/SampleBitronixApplicationTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-bitronix/src/test/java/smoketest/bitronix/SampleBitronixApplicationTests.java @@ -16,8 +16,9 @@ package smoketest.bitronix; +import java.util.function.Consumer; + import bitronix.tm.resource.jms.PoolingConnectionFactory; -import org.assertj.core.api.Condition; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -25,6 +26,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.context.ApplicationContext; +import org.springframework.util.StringUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -39,8 +41,10 @@ class SampleBitronixApplicationTests { @Test void testTransactionRollback(CapturedOutput output) throws Exception { SampleBitronixApplication.main(new String[] {}); - assertThat(output.toString()).has(substring(1, "---->")).has(substring(1, "----> josh")) - .has(substring(2, "Count is 1")).has(substring(1, "Simulated error")); + assertThat(output).satisfies(numberOfOccurrences("---->", 1)); + assertThat(output).satisfies(numberOfOccurrences("----> josh", 1)); + assertThat(output).satisfies(numberOfOccurrences("Count is 1", 2)); + assertThat(output).satisfies(numberOfOccurrences("Simulated error", 1)); } @Test @@ -54,20 +58,10 @@ class SampleBitronixApplicationTests { assertThat(nonXaJmsConnectionFactory).isNotInstanceOf(PoolingConnectionFactory.class); } - private Condition substring(int times, String substring) { - return new Condition("containing '" + substring + "' " + times + " times") { - - @Override - public boolean matches(String value) { - int i = 0; - while (value.contains(substring)) { - int beginIndex = value.indexOf(substring) + substring.length(); - value = value.substring(beginIndex); - i++; - } - return i == times; - } - + private Consumer numberOfOccurrences(String substring, int expectedCount) { + return (charSequence) -> { + int count = StringUtils.countOccurrencesOf(charSequence.toString(), substring); + assertThat(count).isEqualTo(expectedCount); }; }