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");
* 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

@ -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

@ -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'");
});
}

@ -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");
});
}

@ -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");
});
}

@ -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"));
}

@ -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

@ -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);

@ -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'");
}

@ -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<? 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");
* 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");
}

@ -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

@ -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");
}

@ -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");
}

@ -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

Loading…
Cancel
Save