Refine assertions on exception messages

This commit is contained in:
Stephane Nicoll
2022-06-14 18:07:01 +02:00
parent b0f5fb51fc
commit b536b209ab
15 changed files with 47 additions and 45 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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