Refine assertions on exception messages

pull/31400/head
Stephane Nicoll 2 years ago
parent b0f5fb51fc
commit b536b209ab

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -46,8 +46,8 @@ class DiskSpaceHealthContributorAutoConfigurationTests {
@Test @Test
void thresholdMustBePositive() { void thresholdMustBePositive() {
this.contextRunner.withPropertyValues("management.health.diskspace.threshold=-10MB") this.contextRunner.withPropertyValues("management.health.diskspace.threshold=-10MB")
.run((context) -> assertThat(context).hasFailed().getFailure() .run((context) -> assertThat(context).hasFailed().getFailure().rootCause()
.hasMessageContaining("Failed to bind properties under 'management.health.diskspace'")); .hasMessage("threshold must be greater than or equal to 0"));
} }
@Test @Test

@ -125,8 +125,8 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
void notSupportedCachingMode() { void notSupportedCachingMode() {
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
.withPropertyValues("spring.cache.type=foobar") .withPropertyValues("spring.cache.type=foobar")
.run((context) -> assertThat(context).getFailure().isInstanceOf(BeanCreationException.class) .run((context) -> assertThat(context).getFailure().isInstanceOf(BeanCreationException.class).rootCause()
.hasMessageContaining("Failed to bind properties under 'spring.cache.type'")); .hasMessageContaining("No enum constant").hasMessageContaining("foobar"));
} }
@Test @Test

@ -28,6 +28,8 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 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.context.runner.WebApplicationContextRunner;
import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension; 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") this.contextRunner.withPropertyValues("spring.h2.console.enabled=true", "spring.h2.console.path=custom")
.run((context) -> { .run((context) -> {
assertThat(context).hasFailed(); 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'"); .hasMessageContaining("Failed to bind properties under 'spring.h2.console'");
}); });
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -48,10 +48,8 @@ class SessionAutoConfigurationIntegrationTests extends AbstractSessionAutoConfig
void severalCandidatesWithNoSessionStore() { void severalCandidatesWithNoSessionStore() {
this.contextRunner.withUserConfiguration(HazelcastConfiguration.class).run((context) -> { this.contextRunner.withUserConfiguration(HazelcastConfiguration.class).run((context) -> {
assertThat(context).hasFailed(); assertThat(context).hasFailed();
assertThat(context).getFailure().hasRootCauseInstanceOf(NonUniqueSessionRepositoryException.class); assertThat(context).getFailure().rootCause().isInstanceOf(NonUniqueSessionRepositoryException.class)
assertThat(context).getFailure() .hasMessageContaining("Multiple session repository candidates are available")
.hasMessageContaining("Multiple session repository candidates are available");
assertThat(context).getFailure()
.hasMessageContaining("set the 'spring.session.store-type' property accordingly"); .hasMessageContaining("set the 'spring.session.store-type' property accordingly");
}); });
} }

@ -63,8 +63,7 @@ class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurationTest
void contextFailsIfMultipleStoresAreAvailable() { void contextFailsIfMultipleStoresAreAvailable() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
assertThat(context).hasFailed(); assertThat(context).hasFailed();
assertThat(context).getFailure().hasRootCauseInstanceOf(NonUniqueSessionRepositoryException.class); assertThat(context).getFailure().rootCause().isInstanceOf(NonUniqueSessionRepositoryException.class)
assertThat(context).getFailure()
.hasMessageContaining("Multiple session repository candidates are available"); .hasMessageContaining("Multiple session repository candidates are available");
}); });
} }

@ -83,7 +83,7 @@ class ReactiveWebServerFactoryAutoConfigurationTests {
void missingHttpHandler() { void missingHttpHandler() {
this.contextRunner.withUserConfiguration(MockWebServerConfiguration.class) this.contextRunner.withUserConfiguration(MockWebServerConfiguration.class)
.run((context) -> assertThat(context.getStartupFailure()) .run((context) -> assertThat(context.getStartupFailure())
.isInstanceOf(ApplicationContextException.class) .isInstanceOf(ApplicationContextException.class).rootCause()
.hasMessageContaining("missing HttpHandler bean")); .hasMessageContaining("missing HttpHandler bean"));
} }
@ -93,7 +93,7 @@ class ReactiveWebServerFactoryAutoConfigurationTests {
.withUserConfiguration(MockWebServerConfiguration.class, HttpHandlerConfiguration.class, .withUserConfiguration(MockWebServerConfiguration.class, HttpHandlerConfiguration.class,
TooManyHttpHandlers.class) TooManyHttpHandlers.class)
.run((context) -> assertThat(context.getStartupFailure()) .run((context) -> assertThat(context.getStartupFailure())
.isInstanceOf(ApplicationContextException.class) .isInstanceOf(ApplicationContextException.class).rootCause()
.hasMessageContaining("multiple HttpHandler beans : httpHandler,additionalHttpHandler")); .hasMessageContaining("multiple HttpHandler beans : httpHandler,additionalHttpHandler"));
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -51,8 +51,8 @@ class WebServicesAutoConfigurationTests {
@Test @Test
void customPathMustBeginWithASlash() { void customPathMustBeginWithASlash() {
this.contextRunner.withPropertyValues("spring.webservices.path=invalid") this.contextRunner.withPropertyValues("spring.webservices.path=invalid")
.run((context) -> assertThat(context).getFailure().isInstanceOf(BeanCreationException.class) .run((context) -> assertThat(context).getFailure().isInstanceOf(BeanCreationException.class).rootCause()
.hasMessageContaining("Failed to bind properties under 'spring.webservices'")); .hasMessageContaining("Path must start with '/'"));
} }
@Test @Test

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,8 +40,7 @@ class ConfigurationPropertiesBindExceptionTests {
new IllegalStateException()); new IllegalStateException());
assertThat(exception.getMessage()).isEqualTo("Error creating bean with name 'example': " assertThat(exception.getMessage()).isEqualTo("Error creating bean with name 'example': "
+ "Could not bind properties to 'ConfigurationPropertiesBindExceptionTests.Example' : " + "Could not bind properties to 'ConfigurationPropertiesBindExceptionTests.Example' : "
+ "prefix=, ignoreInvalidFields=false, ignoreUnknownFields=true; " + "prefix=, ignoreInvalidFields=false, ignoreUnknownFields=true");
+ "nested exception is java.lang.IllegalStateException");
assertThat(exception.getBeanType()).isEqualTo(Example.class); assertThat(exception.getBeanType()).isEqualTo(Example.class);
assertThat(exception.getBeanName()).isEqualTo("example"); assertThat(exception.getBeanName()).isEqualTo("example");
assertThat(exception.getAnnotation()).isInstanceOf(ConfigurationProperties.class); assertThat(exception.getAnnotation()).isInstanceOf(ConfigurationProperties.class);

@ -749,7 +749,7 @@ class ConfigurationPropertiesTests {
.isThrownBy(() -> load(IgnoreUnknownFieldsFalseConfiguration.class, "name=foo", "bar=baz")) .isThrownBy(() -> load(IgnoreUnknownFieldsFalseConfiguration.class, "name=foo", "bar=baz"))
.withMessageContaining("Could not bind properties to " .withMessageContaining("Could not bind properties to "
+ "'ConfigurationPropertiesTests.IgnoreUnknownFieldsFalseProperties' : " + "'ConfigurationPropertiesTests.IgnoreUnknownFieldsFalseProperties' : "
+ "prefix=, ignoreInvalidFields=false, ignoreUnknownFields=false;"); + "prefix=, ignoreInvalidFields=false, ignoreUnknownFields=false");
} }
@Test @Test
@ -810,8 +810,8 @@ class ConfigurationPropertiesTests {
@Test @Test
void loadWhenConfigurationPropertiesInjectsAnotherBeanShouldNotFail() { void loadWhenConfigurationPropertiesInjectsAnotherBeanShouldNotFail() {
assertThatExceptionOfType(ConfigurationPropertiesBindException.class) assertThatExceptionOfType(ConfigurationPropertiesBindException.class)
.isThrownBy(() -> load(OtherInjectPropertiesConfiguration.class)) .isThrownBy(() -> load(OtherInjectPropertiesConfiguration.class)).havingCause()
.withMessageContaining(OtherInjectedProperties.class.getName()) .isInstanceOf(BindException.class).withMessageContaining(OtherInjectedProperties.class.getName())
.withMessageContaining("Failed to bind properties under 'test'"); .withMessageContaining("Failed to bind properties under 'test'");
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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); InputStreamSource source = mock(InputStreamSource.class);
given(source.getInputStream()).willThrow(IOException.class); given(source.getInputStream()).willThrow(IOException.class);
assertThatExceptionOfType(ConversionFailedException.class) assertThatExceptionOfType(ConversionFailedException.class)
.isThrownBy(() -> conversionService.convert(source, byte[].class)) .isThrownBy(() -> conversionService.convert(source, byte[].class)).havingCause()
.withCauseExactlyInstanceOf(IllegalStateException.class) .isInstanceOf(IllegalStateException.class)
.withMessageContaining("Unable to read from input stream source"); .withMessageContaining("Unable to read from input stream source");
} }
@ -66,9 +66,8 @@ class InputStreamSourceToByteArrayConverterTests {
given(source.getInputStream()).willThrow(IOException.class); given(source.getInputStream()).willThrow(IOException.class);
given(((OriginProvider) source).getOrigin()).willReturn(origin); given(((OriginProvider) source).getOrigin()).willReturn(origin);
assertThatExceptionOfType(ConversionFailedException.class) assertThatExceptionOfType(ConversionFailedException.class)
.isThrownBy(() -> conversionService.convert(source, byte[].class)) .isThrownBy(() -> conversionService.convert(source, byte[].class)).havingCause()
.withCauseExactlyInstanceOf(IllegalStateException.class) .isInstanceOf(IllegalStateException.class).withMessageContaining("Unable to read from mylocation");
.withMessageContaining("Unable to read from mylocation");
} }
@ConversionServiceTest @ConversionServiceTest
@ -78,9 +77,8 @@ class InputStreamSourceToByteArrayConverterTests {
given(source.getInputStream()).willThrow(IOException.class); given(source.getInputStream()).willThrow(IOException.class);
given(source.getDescription()).willReturn("myresource"); given(source.getDescription()).willReturn("myresource");
assertThatExceptionOfType(ConversionFailedException.class) assertThatExceptionOfType(ConversionFailedException.class)
.isThrownBy(() -> conversionService.convert(source, byte[].class)) .isThrownBy(() -> conversionService.convert(source, byte[].class)).havingCause()
.withCauseExactlyInstanceOf(IllegalStateException.class) .isInstanceOf(IllegalStateException.class).withMessageContaining("Unable to read from myresource");
.withMessageContaining("Unable to read from myresource");
} }
static Stream<? extends Arguments> conversionServices() { static Stream<? extends Arguments> conversionServices() {

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -88,6 +88,7 @@ class StringToDataSizeConverterTests {
@ConversionServiceTest @ConversionServiceTest
void convertWhenBadFormatShouldThrowException(ConversionService conversionService) { void convertWhenBadFormatShouldThrowException(ConversionService conversionService) {
assertThatExceptionOfType(ConversionFailedException.class).isThrownBy(() -> convert(conversionService, "10WB")) assertThatExceptionOfType(ConversionFailedException.class).isThrownBy(() -> convert(conversionService, "10WB"))
.havingCause().isInstanceOf(IllegalArgumentException.class)
.withMessageContaining("'10WB' is not a valid data size"); .withMessageContaining("'10WB' is not a valid data size");
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -121,7 +121,7 @@ class StringToDurationConverterTests {
@ConversionServiceTest @ConversionServiceTest
void convertWhenBadFormatShouldThrowException(ConversionService conversionService) { void convertWhenBadFormatShouldThrowException(ConversionService conversionService) {
assertThatExceptionOfType(ConversionFailedException.class).isThrownBy(() -> convert(conversionService, "10foo")) assertThatExceptionOfType(ConversionFailedException.class).isThrownBy(() -> convert(conversionService, "10foo"))
.withMessageContaining("'10foo' is not a valid duration"); .havingRootCause().withMessageContaining("'10foo' is not a valid duration");
} }
@ConversionServiceTest @ConversionServiceTest

@ -60,7 +60,7 @@ class ReactiveWebServerApplicationContextTests {
@Test @Test
void whenThereIsNoWebServerFactoryBeanThenContextRefreshWillFail() { void whenThereIsNoWebServerFactoryBeanThenContextRefreshWillFail() {
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh()) assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
.withMessageContaining( .havingRootCause().withMessageContaining(
"Unable to start ReactiveWebServerApplicationContext due to missing ReactiveWebServerFactory bean"); "Unable to start ReactiveWebServerApplicationContext due to missing ReactiveWebServerFactory bean");
} }
@ -68,6 +68,7 @@ class ReactiveWebServerApplicationContextTests {
void whenThereIsNoHttpHandlerBeanThenContextRefreshWillFail() { void whenThereIsNoHttpHandlerBeanThenContextRefreshWillFail() {
addWebServerFactoryBean(); addWebServerFactoryBean();
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh()) assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
.havingRootCause()
.withMessageContaining("Unable to start ReactiveWebApplicationContext due to missing HttpHandler bean"); .withMessageContaining("Unable to start ReactiveWebApplicationContext due to missing HttpHandler bean");
} }
@ -76,7 +77,7 @@ class ReactiveWebServerApplicationContextTests {
addWebServerFactoryBean(); addWebServerFactoryBean();
addWebServerFactoryBean("anotherWebServerFactory"); addWebServerFactoryBean("anotherWebServerFactory");
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh()) assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
.withMessageContaining( .havingRootCause().withMessageContaining(
"Unable to start ReactiveWebApplicationContext due to multiple ReactiveWebServerFactory beans"); "Unable to start ReactiveWebApplicationContext due to multiple ReactiveWebServerFactory beans");
} }
@ -86,7 +87,7 @@ class ReactiveWebServerApplicationContextTests {
addHttpHandlerBean("httpHandler1"); addHttpHandlerBean("httpHandler1");
addHttpHandlerBean("httpHandler2"); addHttpHandlerBean("httpHandler2");
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh()) assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
.withMessageContaining( .havingRootCause().withMessageContaining(
"Unable to start ReactiveWebApplicationContext due to multiple HttpHandler beans"); "Unable to start ReactiveWebApplicationContext due to multiple HttpHandler beans");
} }

@ -204,6 +204,7 @@ class ServletWebServerApplicationContextTests {
@Test @Test
void missingServletWebServerFactory() { void missingServletWebServerFactory() {
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh()) assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
.havingRootCause()
.withMessageContaining("Unable to start ServletWebServerApplicationContext due to missing " .withMessageContaining("Unable to start ServletWebServerApplicationContext due to missing "
+ "ServletWebServerFactory bean"); + "ServletWebServerFactory bean");
} }
@ -214,7 +215,7 @@ class ServletWebServerApplicationContextTests {
this.context.registerBeanDefinition("webServerFactory2", this.context.registerBeanDefinition("webServerFactory2",
new RootBeanDefinition(MockServletWebServerFactory.class)); new RootBeanDefinition(MockServletWebServerFactory.class));
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh()) 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"); + "multiple ServletWebServerFactory beans");
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.web.ServerProperties; 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.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -56,15 +57,15 @@ class SamplePropertyValidationApplicationTests {
void bindInvalidHost() { void bindInvalidHost() {
this.context.register(SamplePropertyValidationApplication.class); this.context.register(SamplePropertyValidationApplication.class);
TestPropertyValues.of("sample.host:xxxxxx", "sample.port:9090").applyTo(this.context); TestPropertyValues.of("sample.host:xxxxxx", "sample.port:9090").applyTo(this.context);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> this.context.refresh()) assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh).havingRootCause()
.withMessageContaining("Failed to bind properties under 'sample'"); .isInstanceOf(BindValidationException.class);
} }
@Test @Test
void bindNullHost() { void bindNullHost() {
this.context.register(SamplePropertyValidationApplication.class); this.context.register(SamplePropertyValidationApplication.class);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> this.context.refresh()) assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh).havingRootCause()
.withMessageContaining("Failed to bind properties under 'sample'"); .isInstanceOf(BindValidationException.class);
} }
@Test @Test

Loading…
Cancel
Save