From b536b209ab8c6d8a59f52bddcd20b23d61728fda Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 14 Jun 2022 18:07:01 +0200 Subject: [PATCH] Refine assertions on exception messages --- ...eHealthContributorAutoConfigurationTests.java | 6 +++--- .../cache/CacheAutoConfigurationTests.java | 4 ++-- .../h2/H2ConsoleAutoConfigurationTests.java | 6 +++++- ...SessionAutoConfigurationIntegrationTests.java | 8 +++----- .../session/SessionAutoConfigurationTests.java | 3 +-- ...veWebServerFactoryAutoConfigurationTests.java | 4 ++-- .../WebServicesAutoConfigurationTests.java | 6 +++--- ...onfigurationPropertiesBindExceptionTests.java | 5 ++--- .../properties/ConfigurationPropertiesTests.java | 6 +++--- ...putStreamSourceToByteArrayConverterTests.java | 16 +++++++--------- .../convert/StringToDataSizeConverterTests.java | 3 ++- .../convert/StringToDurationConverterTests.java | 4 ++-- ...ReactiveWebServerApplicationContextTests.java | 7 ++++--- .../ServletWebServerApplicationContextTests.java | 3 ++- ...SamplePropertyValidationApplicationTests.java | 11 ++++++----- 15 files changed, 47 insertions(+), 45 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/system/DiskSpaceHealthContributorAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/system/DiskSpaceHealthContributorAutoConfigurationTests.java index 1e5ac554b84..a43fcebe23d 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/system/DiskSpaceHealthContributorAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/system/DiskSpaceHealthContributorAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,8 +46,8 @@ class DiskSpaceHealthContributorAutoConfigurationTests { @Test void thresholdMustBePositive() { this.contextRunner.withPropertyValues("management.health.diskspace.threshold=-10MB") - .run((context) -> assertThat(context).hasFailed().getFailure() - .hasMessageContaining("Failed to bind properties under 'management.health.diskspace'")); + .run((context) -> assertThat(context).hasFailed().getFailure().rootCause() + .hasMessage("threshold must be greater than or equal to 0")); } @Test diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java index 27644c75e13..5ceabc3410e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java @@ -125,8 +125,8 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests { void notSupportedCachingMode() { this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=foobar") - .run((context) -> assertThat(context).getFailure().isInstanceOf(BeanCreationException.class) - .hasMessageContaining("Failed to bind properties under 'spring.cache.type'")); + .run((context) -> assertThat(context).getFailure().isInstanceOf(BeanCreationException.class).rootCause() + .hasMessageContaining("No enum constant").hasMessageContaining("foobar")); } @Test diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java index 1d6c943781d..d19ef856d36 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java @@ -28,6 +28,8 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.context.properties.ConfigurationPropertiesBindException; +import org.springframework.boot.context.properties.bind.BindException; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; @@ -75,7 +77,9 @@ class H2ConsoleAutoConfigurationTests { this.contextRunner.withPropertyValues("spring.h2.console.enabled=true", "spring.h2.console.path=custom") .run((context) -> { assertThat(context).hasFailed(); - assertThat(context.getStartupFailure()).isInstanceOf(BeanCreationException.class) + assertThat(context.getStartupFailure()).isInstanceOf(BeanCreationException.class).cause() + .isInstanceOf(ConfigurationPropertiesBindException.class).cause() + .isInstanceOf(BindException.class) .hasMessageContaining("Failed to bind properties under 'spring.h2.console'"); }); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationIntegrationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationIntegrationTests.java index 186f7602099..81ffebc6cff 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationIntegrationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,10 +48,8 @@ class SessionAutoConfigurationIntegrationTests extends AbstractSessionAutoConfig void severalCandidatesWithNoSessionStore() { this.contextRunner.withUserConfiguration(HazelcastConfiguration.class).run((context) -> { assertThat(context).hasFailed(); - assertThat(context).getFailure().hasRootCauseInstanceOf(NonUniqueSessionRepositoryException.class); - assertThat(context).getFailure() - .hasMessageContaining("Multiple session repository candidates are available"); - assertThat(context).getFailure() + assertThat(context).getFailure().rootCause().isInstanceOf(NonUniqueSessionRepositoryException.class) + .hasMessageContaining("Multiple session repository candidates are available") .hasMessageContaining("set the 'spring.session.store-type' property accordingly"); }); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java index 70159d83874..f6754c67c8c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java @@ -63,8 +63,7 @@ class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurationTest void contextFailsIfMultipleStoresAreAvailable() { this.contextRunner.run((context) -> { assertThat(context).hasFailed(); - assertThat(context).getFailure().hasRootCauseInstanceOf(NonUniqueSessionRepositoryException.class); - assertThat(context).getFailure() + assertThat(context).getFailure().rootCause().isInstanceOf(NonUniqueSessionRepositoryException.class) .hasMessageContaining("Multiple session repository candidates are available"); }); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfigurationTests.java index 4b549494d60..ba3b12b2f77 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfigurationTests.java @@ -83,7 +83,7 @@ class ReactiveWebServerFactoryAutoConfigurationTests { void missingHttpHandler() { this.contextRunner.withUserConfiguration(MockWebServerConfiguration.class) .run((context) -> assertThat(context.getStartupFailure()) - .isInstanceOf(ApplicationContextException.class) + .isInstanceOf(ApplicationContextException.class).rootCause() .hasMessageContaining("missing HttpHandler bean")); } @@ -93,7 +93,7 @@ class ReactiveWebServerFactoryAutoConfigurationTests { .withUserConfiguration(MockWebServerConfiguration.class, HttpHandlerConfiguration.class, TooManyHttpHandlers.class) .run((context) -> assertThat(context.getStartupFailure()) - .isInstanceOf(ApplicationContextException.class) + .isInstanceOf(ApplicationContextException.class).rootCause() .hasMessageContaining("multiple HttpHandler beans : httpHandler,additionalHttpHandler")); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java index 27d64c3afca..c160a64887c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,8 +51,8 @@ class WebServicesAutoConfigurationTests { @Test void customPathMustBeginWithASlash() { this.contextRunner.withPropertyValues("spring.webservices.path=invalid") - .run((context) -> assertThat(context).getFailure().isInstanceOf(BeanCreationException.class) - .hasMessageContaining("Failed to bind properties under 'spring.webservices'")); + .run((context) -> assertThat(context).getFailure().isInstanceOf(BeanCreationException.class).rootCause() + .hasMessageContaining("Path must start with '/'")); } @Test diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindExceptionTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindExceptionTests.java index 23290e122f0..a658b56a167 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindExceptionTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindExceptionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,8 +40,7 @@ class ConfigurationPropertiesBindExceptionTests { new IllegalStateException()); assertThat(exception.getMessage()).isEqualTo("Error creating bean with name 'example': " + "Could not bind properties to 'ConfigurationPropertiesBindExceptionTests.Example' : " - + "prefix=, ignoreInvalidFields=false, ignoreUnknownFields=true; " - + "nested exception is java.lang.IllegalStateException"); + + "prefix=, ignoreInvalidFields=false, ignoreUnknownFields=true"); assertThat(exception.getBeanType()).isEqualTo(Example.class); assertThat(exception.getBeanName()).isEqualTo("example"); assertThat(exception.getAnnotation()).isInstanceOf(ConfigurationProperties.class); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java index 3743dd1f7d0..d4d03fc1a1b 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java @@ -749,7 +749,7 @@ class ConfigurationPropertiesTests { .isThrownBy(() -> load(IgnoreUnknownFieldsFalseConfiguration.class, "name=foo", "bar=baz")) .withMessageContaining("Could not bind properties to " + "'ConfigurationPropertiesTests.IgnoreUnknownFieldsFalseProperties' : " - + "prefix=, ignoreInvalidFields=false, ignoreUnknownFields=false;"); + + "prefix=, ignoreInvalidFields=false, ignoreUnknownFields=false"); } @Test @@ -810,8 +810,8 @@ class ConfigurationPropertiesTests { @Test void loadWhenConfigurationPropertiesInjectsAnotherBeanShouldNotFail() { assertThatExceptionOfType(ConfigurationPropertiesBindException.class) - .isThrownBy(() -> load(OtherInjectPropertiesConfiguration.class)) - .withMessageContaining(OtherInjectedProperties.class.getName()) + .isThrownBy(() -> load(OtherInjectPropertiesConfiguration.class)).havingCause() + .isInstanceOf(BindException.class).withMessageContaining(OtherInjectedProperties.class.getName()) .withMessageContaining("Failed to bind properties under 'test'"); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/InputStreamSourceToByteArrayConverterTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/InputStreamSourceToByteArrayConverterTests.java index d3683e9361a..47eb7f98427 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/InputStreamSourceToByteArrayConverterTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/InputStreamSourceToByteArrayConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,8 +53,8 @@ class InputStreamSourceToByteArrayConverterTests { InputStreamSource source = mock(InputStreamSource.class); given(source.getInputStream()).willThrow(IOException.class); assertThatExceptionOfType(ConversionFailedException.class) - .isThrownBy(() -> conversionService.convert(source, byte[].class)) - .withCauseExactlyInstanceOf(IllegalStateException.class) + .isThrownBy(() -> conversionService.convert(source, byte[].class)).havingCause() + .isInstanceOf(IllegalStateException.class) .withMessageContaining("Unable to read from input stream source"); } @@ -66,9 +66,8 @@ class InputStreamSourceToByteArrayConverterTests { given(source.getInputStream()).willThrow(IOException.class); given(((OriginProvider) source).getOrigin()).willReturn(origin); assertThatExceptionOfType(ConversionFailedException.class) - .isThrownBy(() -> conversionService.convert(source, byte[].class)) - .withCauseExactlyInstanceOf(IllegalStateException.class) - .withMessageContaining("Unable to read from mylocation"); + .isThrownBy(() -> conversionService.convert(source, byte[].class)).havingCause() + .isInstanceOf(IllegalStateException.class).withMessageContaining("Unable to read from mylocation"); } @ConversionServiceTest @@ -78,9 +77,8 @@ class InputStreamSourceToByteArrayConverterTests { given(source.getInputStream()).willThrow(IOException.class); given(source.getDescription()).willReturn("myresource"); assertThatExceptionOfType(ConversionFailedException.class) - .isThrownBy(() -> conversionService.convert(source, byte[].class)) - .withCauseExactlyInstanceOf(IllegalStateException.class) - .withMessageContaining("Unable to read from myresource"); + .isThrownBy(() -> conversionService.convert(source, byte[].class)).havingCause() + .isInstanceOf(IllegalStateException.class).withMessageContaining("Unable to read from myresource"); } static Stream conversionServices() { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/StringToDataSizeConverterTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/StringToDataSizeConverterTests.java index 0855db899d1..1a6fb701db9 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/StringToDataSizeConverterTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/StringToDataSizeConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -88,6 +88,7 @@ class StringToDataSizeConverterTests { @ConversionServiceTest void convertWhenBadFormatShouldThrowException(ConversionService conversionService) { assertThatExceptionOfType(ConversionFailedException.class).isThrownBy(() -> convert(conversionService, "10WB")) + .havingCause().isInstanceOf(IllegalArgumentException.class) .withMessageContaining("'10WB' is not a valid data size"); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/StringToDurationConverterTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/StringToDurationConverterTests.java index 4ffad3e6cc2..4555ec8311a 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/StringToDurationConverterTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/StringToDurationConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -121,7 +121,7 @@ class StringToDurationConverterTests { @ConversionServiceTest void convertWhenBadFormatShouldThrowException(ConversionService conversionService) { assertThatExceptionOfType(ConversionFailedException.class).isThrownBy(() -> convert(conversionService, "10foo")) - .withMessageContaining("'10foo' is not a valid duration"); + .havingRootCause().withMessageContaining("'10foo' is not a valid duration"); } @ConversionServiceTest diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/context/ReactiveWebServerApplicationContextTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/context/ReactiveWebServerApplicationContextTests.java index 13ae884265c..f869a62d77a 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/context/ReactiveWebServerApplicationContextTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/context/ReactiveWebServerApplicationContextTests.java @@ -60,7 +60,7 @@ class ReactiveWebServerApplicationContextTests { @Test void whenThereIsNoWebServerFactoryBeanThenContextRefreshWillFail() { assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh()) - .withMessageContaining( + .havingRootCause().withMessageContaining( "Unable to start ReactiveWebServerApplicationContext due to missing ReactiveWebServerFactory bean"); } @@ -68,6 +68,7 @@ class ReactiveWebServerApplicationContextTests { void whenThereIsNoHttpHandlerBeanThenContextRefreshWillFail() { addWebServerFactoryBean(); assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh()) + .havingRootCause() .withMessageContaining("Unable to start ReactiveWebApplicationContext due to missing HttpHandler bean"); } @@ -76,7 +77,7 @@ class ReactiveWebServerApplicationContextTests { addWebServerFactoryBean(); addWebServerFactoryBean("anotherWebServerFactory"); assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh()) - .withMessageContaining( + .havingRootCause().withMessageContaining( "Unable to start ReactiveWebApplicationContext due to multiple ReactiveWebServerFactory beans"); } @@ -86,7 +87,7 @@ class ReactiveWebServerApplicationContextTests { addHttpHandlerBean("httpHandler1"); addHttpHandlerBean("httpHandler2"); assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh()) - .withMessageContaining( + .havingRootCause().withMessageContaining( "Unable to start ReactiveWebApplicationContext due to multiple HttpHandler beans"); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/context/ServletWebServerApplicationContextTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/context/ServletWebServerApplicationContextTests.java index cd0a492d2dc..426512ffe63 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/context/ServletWebServerApplicationContextTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/context/ServletWebServerApplicationContextTests.java @@ -204,6 +204,7 @@ class ServletWebServerApplicationContextTests { @Test void missingServletWebServerFactory() { assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh()) + .havingRootCause() .withMessageContaining("Unable to start ServletWebServerApplicationContext due to missing " + "ServletWebServerFactory bean"); } @@ -214,7 +215,7 @@ class ServletWebServerApplicationContextTests { this.context.registerBeanDefinition("webServerFactory2", new RootBeanDefinition(MockServletWebServerFactory.class)); assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh()) - .withMessageContaining("Unable to start ServletWebServerApplicationContext due to " + .havingRootCause().withMessageContaining("Unable to start ServletWebServerApplicationContext due to " + "multiple ServletWebServerFactory beans"); } diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-property-validation/src/test/java/smoketest/propertyvalidation/SamplePropertyValidationApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-property-validation/src/test/java/smoketest/propertyvalidation/SamplePropertyValidationApplicationTests.java index 4e6919b7e6c..4ddb619d116 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-property-validation/src/test/java/smoketest/propertyvalidation/SamplePropertyValidationApplicationTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-property-validation/src/test/java/smoketest/propertyvalidation/SamplePropertyValidationApplicationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.context.properties.bind.validation.BindValidationException; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -56,15 +57,15 @@ class SamplePropertyValidationApplicationTests { void bindInvalidHost() { this.context.register(SamplePropertyValidationApplication.class); TestPropertyValues.of("sample.host:xxxxxx", "sample.port:9090").applyTo(this.context); - assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> this.context.refresh()) - .withMessageContaining("Failed to bind properties under 'sample'"); + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh).havingRootCause() + .isInstanceOf(BindValidationException.class); } @Test void bindNullHost() { this.context.register(SamplePropertyValidationApplication.class); - assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> this.context.refresh()) - .withMessageContaining("Failed to bind properties under 'sample'"); + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh).havingRootCause() + .isInstanceOf(BindValidationException.class); } @Test