Use AssertJ’s exception assertions rather than fail
Closes gh-15761
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-2019 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.
|
||||
@@ -107,7 +107,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.ArgumentMatchers.isA;
|
||||
import static org.mockito.BDDMockito.willThrow;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
@@ -770,17 +769,11 @@ public class SpringApplicationTests {
|
||||
ExitStatusException failure = new ExitStatusException();
|
||||
willThrow(failure).given(listener)
|
||||
.onApplicationEvent(isA(ApplicationReadyEvent.class));
|
||||
try {
|
||||
application.run();
|
||||
fail("Run should have failed with a RuntimeException");
|
||||
}
|
||||
catch (RuntimeException ex) {
|
||||
verify(listener).onApplicationEvent(isA(ApplicationReadyEvent.class));
|
||||
verify(listener, never())
|
||||
.onApplicationEvent(isA(ApplicationFailedEvent.class));
|
||||
assertThat(exitCodeListener.getExitCode()).isEqualTo(11);
|
||||
assertThat(this.output.toString()).contains("Application run failed");
|
||||
}
|
||||
assertThatExceptionOfType(RuntimeException.class).isThrownBy(application::run);
|
||||
verify(listener).onApplicationEvent(isA(ApplicationReadyEvent.class));
|
||||
verify(listener, never()).onApplicationEvent(isA(ApplicationFailedEvent.class));
|
||||
assertThat(exitCodeListener.getExitCode()).isEqualTo(11);
|
||||
assertThat(this.output.toString()).contains("Application run failed");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -855,12 +848,7 @@ public class SpringApplicationTests {
|
||||
ExitCodeListener listener = new ExitCodeListener();
|
||||
application.addListeners(listener);
|
||||
application.setWebApplicationType(WebApplicationType.NONE);
|
||||
try {
|
||||
application.run();
|
||||
fail("Did not throw");
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
}
|
||||
assertThatIllegalStateException().isThrownBy(application::run);
|
||||
verify(handler).registerExitCode(11);
|
||||
assertThat(listener.getExitCode()).isEqualTo(11);
|
||||
}
|
||||
@@ -880,12 +868,7 @@ public class SpringApplicationTests {
|
||||
ExitCodeListener listener = new ExitCodeListener();
|
||||
application.addListeners(listener);
|
||||
application.setWebApplicationType(WebApplicationType.NONE);
|
||||
try {
|
||||
application.run();
|
||||
fail("Did not throw");
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
}
|
||||
assertThatIllegalStateException().isThrownBy(application::run);
|
||||
verify(handler).registerExitCode(11);
|
||||
assertThat(listener.getExitCode()).isEqualTo(11);
|
||||
}
|
||||
@@ -905,12 +888,7 @@ public class SpringApplicationTests {
|
||||
ExitCodeListener listener = new ExitCodeListener();
|
||||
application.addListeners(listener);
|
||||
application.setWebApplicationType(WebApplicationType.NONE);
|
||||
try {
|
||||
application.run();
|
||||
fail("Did not throw");
|
||||
}
|
||||
catch (RuntimeException ex) {
|
||||
}
|
||||
assertThatExceptionOfType(RuntimeException.class).isThrownBy(application::run);
|
||||
ArgumentCaptor<RuntimeException> exceptionCaptor = ArgumentCaptor
|
||||
.forClass(RuntimeException.class);
|
||||
verify(handler).registerLoggedException(exceptionCaptor.capture());
|
||||
@@ -998,16 +976,12 @@ public class SpringApplicationTests {
|
||||
ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class);
|
||||
SpringApplication application = new SpringApplication(ExampleConfig.class);
|
||||
application.addListeners(listener);
|
||||
try {
|
||||
application.run();
|
||||
fail("Run should have failed with an ApplicationContextException");
|
||||
}
|
||||
catch (ApplicationContextException ex) {
|
||||
verifyListenerEvents(listener, ApplicationStartingEvent.class,
|
||||
ApplicationEnvironmentPreparedEvent.class,
|
||||
ApplicationContextInitializedEvent.class,
|
||||
ApplicationPreparedEvent.class, ApplicationFailedEvent.class);
|
||||
}
|
||||
assertThatExceptionOfType(ApplicationContextException.class)
|
||||
.isThrownBy(application::run);
|
||||
verifyListenerEvents(listener, ApplicationStartingEvent.class,
|
||||
ApplicationEnvironmentPreparedEvent.class,
|
||||
ApplicationContextInitializedEvent.class, ApplicationPreparedEvent.class,
|
||||
ApplicationFailedEvent.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -1018,16 +992,12 @@ public class SpringApplicationTests {
|
||||
BrokenPostConstructConfig.class);
|
||||
application.setWebApplicationType(WebApplicationType.NONE);
|
||||
application.addListeners(listener);
|
||||
try {
|
||||
application.run();
|
||||
fail("Run should have failed with a BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
verifyListenerEvents(listener, ApplicationStartingEvent.class,
|
||||
ApplicationEnvironmentPreparedEvent.class,
|
||||
ApplicationContextInitializedEvent.class,
|
||||
ApplicationPreparedEvent.class, ApplicationFailedEvent.class);
|
||||
}
|
||||
assertThatExceptionOfType(BeanCreationException.class)
|
||||
.isThrownBy(application::run);
|
||||
verifyListenerEvents(listener, ApplicationStartingEvent.class,
|
||||
ApplicationEnvironmentPreparedEvent.class,
|
||||
ApplicationContextInitializedEvent.class, ApplicationPreparedEvent.class,
|
||||
ApplicationFailedEvent.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -1038,13 +1008,9 @@ public class SpringApplicationTests {
|
||||
SpringApplication application = new SpringApplication(ExampleConfig.class);
|
||||
application.addInitializers((applicationContext) -> applicationContext
|
||||
.addApplicationListener(listener));
|
||||
try {
|
||||
application.run();
|
||||
fail("Run should have failed with an ApplicationContextException");
|
||||
}
|
||||
catch (ApplicationContextException ex) {
|
||||
verifyListenerEvents(listener, ApplicationFailedEvent.class);
|
||||
}
|
||||
assertThatExceptionOfType(ApplicationContextException.class)
|
||||
.isThrownBy(application::run);
|
||||
verifyListenerEvents(listener, ApplicationFailedEvent.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -1056,13 +1022,9 @@ public class SpringApplicationTests {
|
||||
application.setWebApplicationType(WebApplicationType.NONE);
|
||||
application.addInitializers((applicationContext) -> applicationContext
|
||||
.addApplicationListener(listener));
|
||||
try {
|
||||
application.run();
|
||||
fail("Run should have failed with a BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
verifyListenerEvents(listener, ApplicationFailedEvent.class);
|
||||
}
|
||||
assertThatExceptionOfType(BeanCreationException.class)
|
||||
.isThrownBy(application::run);
|
||||
verifyListenerEvents(listener, ApplicationFailedEvent.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-2019 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.
|
||||
@@ -82,7 +82,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
@@ -702,15 +701,13 @@ public class ConfigurationPropertiesTests {
|
||||
|
||||
@Test
|
||||
public void loadWhenHasConfigurationPropertiesValidatorShouldApplyValidator() {
|
||||
try {
|
||||
load(WithCustomValidatorConfiguration.class);
|
||||
fail("Did not throw");
|
||||
}
|
||||
catch (Exception ex) {
|
||||
assertThat(ex).hasCauseInstanceOf(BindException.class);
|
||||
assertThat(ex.getCause())
|
||||
.hasCauseExactlyInstanceOf(BindValidationException.class);
|
||||
}
|
||||
assertThatExceptionOfType(Exception.class)
|
||||
.isThrownBy(() -> load(WithCustomValidatorConfiguration.class))
|
||||
.satisfies((ex) -> {
|
||||
assertThat(ex).hasCauseInstanceOf(BindException.class);
|
||||
assertThat(ex.getCause())
|
||||
.hasCauseExactlyInstanceOf(BindValidationException.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -723,15 +720,12 @@ public class ConfigurationPropertiesTests {
|
||||
|
||||
@Test
|
||||
public void loadWhenConfigurationPropertiesIsAlsoValidatorShouldApplyValidator() {
|
||||
try {
|
||||
load(ValidatorProperties.class);
|
||||
fail("Did not throw");
|
||||
}
|
||||
catch (Exception ex) {
|
||||
assertThat(ex).hasCauseInstanceOf(BindException.class);
|
||||
assertThat(ex.getCause())
|
||||
.hasCauseExactlyInstanceOf(BindValidationException.class);
|
||||
}
|
||||
assertThatExceptionOfType(Exception.class)
|
||||
.isThrownBy(() -> load(ValidatorProperties.class)).satisfies((ex) -> {
|
||||
assertThat(ex).hasCauseInstanceOf(BindException.class);
|
||||
assertThat(ex.getCause())
|
||||
.hasCauseExactlyInstanceOf(BindValidationException.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-2019 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.
|
||||
@@ -32,7 +32,7 @@ import org.springframework.boot.context.properties.source.MockConfigurationPrope
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isA;
|
||||
@@ -145,18 +145,16 @@ public class ArrayBinderTests {
|
||||
source.put("foo[1]", "1");
|
||||
source.put("foo[3]", "3");
|
||||
this.sources.add(source);
|
||||
try {
|
||||
this.binder.bind("foo", INTEGER_ARRAY);
|
||||
fail("No exception thrown");
|
||||
}
|
||||
catch (BindException ex) {
|
||||
Set<ConfigurationProperty> unbound = ((UnboundConfigurationPropertiesException) ex
|
||||
.getCause()).getUnboundProperties();
|
||||
assertThat(unbound.size()).isEqualTo(1);
|
||||
ConfigurationProperty property = unbound.iterator().next();
|
||||
assertThat(property.getName().toString()).isEqualTo("foo[3]");
|
||||
assertThat(property.getValue()).isEqualTo("3");
|
||||
}
|
||||
assertThatExceptionOfType(BindException.class)
|
||||
.isThrownBy(() -> this.binder.bind("foo", INTEGER_ARRAY))
|
||||
.satisfies((ex) -> {
|
||||
Set<ConfigurationProperty> unbound = ((UnboundConfigurationPropertiesException) ex
|
||||
.getCause()).getUnboundProperties();
|
||||
assertThat(unbound.size()).isEqualTo(1);
|
||||
ConfigurationProperty property = unbound.iterator().next();
|
||||
assertThat(property.getName().toString()).isEqualTo("foo[3]");
|
||||
assertThat(property.getValue()).isEqualTo("3");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -38,7 +38,7 @@ import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.test.context.support.TestPropertySourceUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests for {@link CollectionBinder}.
|
||||
@@ -121,18 +121,16 @@ public class CollectionBinderTests {
|
||||
source.put("foo[1]", "1");
|
||||
source.put("foo[3]", "3");
|
||||
this.sources.add(source);
|
||||
try {
|
||||
this.binder.bind("foo", INTEGER_LIST);
|
||||
fail("No exception thrown");
|
||||
}
|
||||
catch (BindException ex) {
|
||||
Set<ConfigurationProperty> unbound = ((UnboundConfigurationPropertiesException) ex
|
||||
.getCause()).getUnboundProperties();
|
||||
assertThat(unbound).hasSize(1);
|
||||
ConfigurationProperty property = unbound.iterator().next();
|
||||
assertThat(property.getName().toString()).isEqualTo("foo[3]");
|
||||
assertThat(property.getValue()).isEqualTo("3");
|
||||
}
|
||||
assertThatExceptionOfType(BindException.class)
|
||||
.isThrownBy(() -> this.binder.bind("foo", INTEGER_LIST))
|
||||
.satisfies((ex) -> {
|
||||
Set<ConfigurationProperty> unbound = ((UnboundConfigurationPropertiesException) ex
|
||||
.getCause()).getUnboundProperties();
|
||||
assertThat(unbound).hasSize(1);
|
||||
ConfigurationProperty property = unbound.iterator().next();
|
||||
assertThat(property.getName().toString()).isEqualTo("foo[3]");
|
||||
assertThat(property.getValue()).isEqualTo("3");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -142,19 +140,16 @@ public class CollectionBinderTests {
|
||||
source.put("foo[1].value", "2");
|
||||
source.put("foo[4].value", "4");
|
||||
this.sources.add(source);
|
||||
try {
|
||||
Bindable<List<JavaBean>> target = Bindable.listOf(JavaBean.class);
|
||||
this.binder.bind("foo", target);
|
||||
fail("No exception thrown");
|
||||
}
|
||||
catch (BindException ex) {
|
||||
Set<ConfigurationProperty> unbound = ((UnboundConfigurationPropertiesException) ex
|
||||
.getCause()).getUnboundProperties();
|
||||
assertThat(unbound).hasSize(1);
|
||||
ConfigurationProperty property = unbound.iterator().next();
|
||||
assertThat(property.getName().toString()).isEqualTo("foo[4].value");
|
||||
assertThat(property.getValue()).isEqualTo("4");
|
||||
}
|
||||
Bindable<List<JavaBean>> target = Bindable.listOf(JavaBean.class);
|
||||
assertThatExceptionOfType(BindException.class)
|
||||
.isThrownBy(() -> this.binder.bind("foo", target)).satisfies((ex) -> {
|
||||
Set<ConfigurationProperty> unbound = ((UnboundConfigurationPropertiesException) ex
|
||||
.getCause()).getUnboundProperties();
|
||||
assertThat(unbound).hasSize(1);
|
||||
ConfigurationProperty property = unbound.iterator().next();
|
||||
assertThat(property.getName().toString()).isEqualTo("foo[4].value");
|
||||
assertThat(property.getValue()).isEqualTo("4");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2019 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.
|
||||
@@ -30,7 +30,7 @@ import org.springframework.boot.context.properties.source.ConfigurationPropertyS
|
||||
import org.springframework.boot.context.properties.source.MockConfigurationPropertySource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests for {@link NoUnboundElementsBindHandler}.
|
||||
@@ -75,15 +75,11 @@ public class NoUnboundElementsBindHandlerTests {
|
||||
source.put("example.baz", "bar");
|
||||
this.sources.add(source);
|
||||
this.binder = new Binder(this.sources);
|
||||
try {
|
||||
this.binder.bind("example", Bindable.of(Example.class),
|
||||
new NoUnboundElementsBindHandler());
|
||||
fail("did not throw");
|
||||
}
|
||||
catch (BindException ex) {
|
||||
assertThat(ex.getCause().getMessage())
|
||||
.contains("The elements [example.baz] were left unbound");
|
||||
}
|
||||
assertThatExceptionOfType(BindException.class)
|
||||
.isThrownBy(() -> this.binder.bind("example", Bindable.of(Example.class),
|
||||
new NoUnboundElementsBindHandler()))
|
||||
.satisfies((ex) -> assertThat(ex.getCause().getMessage())
|
||||
.contains("The elements [example.baz] were left unbound"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-2019 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.
|
||||
@@ -28,7 +28,6 @@ import org.springframework.boot.context.properties.source.ConfigurationPropertyN
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Tests for {@link ConfigurationPropertyName}.
|
||||
@@ -78,13 +77,9 @@ public class ConfigurationPropertyNameTests {
|
||||
public void ofNameShouldNotContainInvalidChars() {
|
||||
String invalid = "_@$%*+=':;";
|
||||
for (char c : invalid.toCharArray()) {
|
||||
try {
|
||||
ConfigurationPropertyName.of("foo" + c);
|
||||
fail("Did not throw for invalid char " + c);
|
||||
}
|
||||
catch (InvalidConfigurationPropertyNameException ex) {
|
||||
assertThat(ex.getMessage()).contains("is not valid");
|
||||
}
|
||||
assertThatExceptionOfType(InvalidConfigurationPropertyNameException.class)
|
||||
.isThrownBy(() -> ConfigurationPropertyName.of("foo" + c)).satisfies(
|
||||
(ex) -> assertThat(ex.getMessage()).contains("is not valid"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-2019 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.
|
||||
@@ -23,7 +23,6 @@ import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Tests for {@link DurationStyle}.
|
||||
@@ -243,13 +242,10 @@ public class DurationStyleTests {
|
||||
|
||||
@Test
|
||||
public void parseSimpleWhenUnknownUnitShouldThrowException() {
|
||||
try {
|
||||
DurationStyle.SIMPLE.parse("10mb");
|
||||
fail("Did not throw");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
assertThat(ex.getCause().getMessage()).isEqualTo("Unknown unit 'mb'");
|
||||
}
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> DurationStyle.SIMPLE.parse("10mb"))
|
||||
.satisfies((ex) -> assertThat(ex.getCause().getMessage())
|
||||
.isEqualTo("Unknown unit 'mb'"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2019 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.
|
||||
@@ -28,7 +28,7 @@ import org.springframework.boot.web.server.PortInUseException;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link FailureAnalyzers}.
|
||||
@@ -42,15 +42,10 @@ public class FailureAnalyzersIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void analysisIsPerformed() {
|
||||
try {
|
||||
new SpringApplicationBuilder(TestConfiguration.class)
|
||||
.web(WebApplicationType.NONE).run();
|
||||
fail("Application started successfully");
|
||||
}
|
||||
catch (Exception ex) {
|
||||
assertThat(this.outputCapture.toString())
|
||||
.contains("APPLICATION FAILED TO START");
|
||||
}
|
||||
assertThatExceptionOfType(Exception.class)
|
||||
.isThrownBy(() -> new SpringApplicationBuilder(TestConfiguration.class)
|
||||
.web(WebApplicationType.NONE).run());
|
||||
assertThat(this.outputCapture.toString()).contains("APPLICATION FAILED TO START");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-2019 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,14 +21,13 @@ import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.diagnostics.FailureAnalysis;
|
||||
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
|
||||
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests for {@link ValidationExceptionFailureAnalyzer}
|
||||
@@ -41,15 +40,12 @@ public class ValidationExceptionFailureAnalyzerTests {
|
||||
|
||||
@Test
|
||||
public void validatedPropertiesTest() {
|
||||
try {
|
||||
new AnnotationConfigApplicationContext(TestConfiguration.class).close();
|
||||
fail("Expected failure did not occur");
|
||||
}
|
||||
catch (Exception ex) {
|
||||
FailureAnalysis analysis = new ValidationExceptionFailureAnalyzer()
|
||||
.analyze(ex);
|
||||
assertThat(analysis).isNotNull();
|
||||
}
|
||||
assertThatExceptionOfType(Exception.class)
|
||||
.isThrownBy(() -> new AnnotationConfigApplicationContext(
|
||||
TestConfiguration.class).close())
|
||||
.satisfies((ex) -> assertThat(
|
||||
new ValidationExceptionFailureAnalyzer().analyze(ex))
|
||||
.isNotNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.springframework.boot.web.server.Ssl;
|
||||
import org.springframework.boot.web.server.WebServerException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests for {@link SslServerCustomizer}.
|
||||
@@ -85,13 +86,14 @@ public class SslServerCustomizerTests {
|
||||
throws Exception {
|
||||
Ssl ssl = new Ssl();
|
||||
SslServerCustomizer customizer = new SslServerCustomizer(null, ssl, null, null);
|
||||
try {
|
||||
customizer.configureSsl(new SslContextFactory(), ssl, null);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
assertThat(ex).isInstanceOf(WebServerException.class);
|
||||
assertThat(ex).hasMessageContaining("Could not load key store 'null'");
|
||||
}
|
||||
assertThatExceptionOfType(Exception.class)
|
||||
.isThrownBy(
|
||||
() -> customizer.configureSsl(new SslContextFactory(), ssl, null))
|
||||
.satisfies((ex) -> {
|
||||
assertThat(ex).isInstanceOf(WebServerException.class);
|
||||
assertThat(ex)
|
||||
.hasMessageContaining("Could not load key store 'null'");
|
||||
});
|
||||
}
|
||||
|
||||
private Server createCustomizedServer() {
|
||||
|
||||
@@ -23,8 +23,7 @@ import org.junit.Test;
|
||||
import org.springframework.boot.web.server.Ssl;
|
||||
import org.springframework.boot.web.server.WebServerException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
/**
|
||||
* Tests for {@link SslServerCustomizer}.
|
||||
@@ -41,15 +40,10 @@ public class SslServerCustomizerTests {
|
||||
ssl.setKeyStore("src/test/resources/test.jks");
|
||||
ssl.setKeyStoreProvider("com.example.KeyStoreProvider");
|
||||
SslServerCustomizer customizer = new SslServerCustomizer(ssl, null, null);
|
||||
try {
|
||||
customizer.getKeyManagerFactory(ssl, null);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
Throwable cause = ex.getCause();
|
||||
assertThat(cause).isInstanceOf(NoSuchProviderException.class);
|
||||
assertThat(cause).hasMessageContaining("com.example.KeyStoreProvider");
|
||||
}
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> customizer.getKeyManagerFactory(ssl, null))
|
||||
.withCauseInstanceOf(NoSuchProviderException.class)
|
||||
.withMessageContaining("com.example.KeyStoreProvider");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -59,15 +53,10 @@ public class SslServerCustomizerTests {
|
||||
ssl.setTrustStore("src/test/resources/test.jks");
|
||||
ssl.setTrustStoreProvider("com.example.TrustStoreProvider");
|
||||
SslServerCustomizer customizer = new SslServerCustomizer(ssl, null, null);
|
||||
try {
|
||||
customizer.getTrustManagerFactory(ssl, null);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
Throwable cause = ex.getCause();
|
||||
assertThat(cause).isInstanceOf(NoSuchProviderException.class);
|
||||
assertThat(cause).hasMessageContaining("com.example.TrustStoreProvider");
|
||||
}
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> customizer.getTrustManagerFactory(ssl, null))
|
||||
.withCauseInstanceOf(NoSuchProviderException.class)
|
||||
.withMessageContaining("com.example.TrustStoreProvider");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -75,15 +64,10 @@ public class SslServerCustomizerTests {
|
||||
throws Exception {
|
||||
Ssl ssl = new Ssl();
|
||||
SslServerCustomizer customizer = new SslServerCustomizer(ssl, null, null);
|
||||
try {
|
||||
customizer.getKeyManagerFactory(ssl, null);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
Throwable cause = ex.getCause();
|
||||
assertThat(cause).isInstanceOf(WebServerException.class);
|
||||
assertThat(cause).hasMessageContaining("Could not load key store 'null'");
|
||||
}
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> customizer.getKeyManagerFactory(ssl, null))
|
||||
.withCauseInstanceOf(WebServerException.class)
|
||||
.withMessageContaining("Could not load key store 'null'");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ import org.springframework.core.io.Resource;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
@@ -193,15 +193,10 @@ public class SslConnectorCustomizerTests {
|
||||
|
||||
@Test
|
||||
public void customizeWhenSslIsEnabledWithNoKeyStoreThrowsWebServerException() {
|
||||
try {
|
||||
new SslConnectorCustomizer(new Ssl(), null)
|
||||
.customize(this.tomcat.getConnector());
|
||||
fail();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
assertThat(ex).isInstanceOf(WebServerException.class);
|
||||
assertThat(ex).hasMessageContaining("Could not load key store 'null'");
|
||||
}
|
||||
assertThatExceptionOfType(WebServerException.class)
|
||||
.isThrownBy(() -> new SslConnectorCustomizer(new Ssl(), null)
|
||||
.customize(this.tomcat.getConnector()))
|
||||
.withMessageContaining("Could not load key store 'null'");
|
||||
}
|
||||
|
||||
private KeyStore loadStore() throws KeyStoreException, IOException,
|
||||
|
||||
@@ -65,7 +65,6 @@ import org.springframework.test.util.ReflectionTestUtils;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -277,19 +276,14 @@ public class TomcatServletWebServerFactoryTests
|
||||
}
|
||||
|
||||
@Test
|
||||
public void primaryConnectorPortClashThrowsIllegalStateException()
|
||||
throws IOException {
|
||||
public void primaryConnectorPortClashThrowsWebServerException() throws IOException {
|
||||
doWithBlockedPort((port) -> {
|
||||
TomcatServletWebServerFactory factory = getFactory();
|
||||
factory.setPort(port);
|
||||
try {
|
||||
assertThatExceptionOfType(WebServerException.class).isThrownBy(() -> {
|
||||
this.webServer = factory.getWebServer();
|
||||
this.webServer.start();
|
||||
fail();
|
||||
}
|
||||
catch (WebServerException ex) {
|
||||
// Ignore
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ import org.springframework.boot.web.server.WebServerException;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
/**
|
||||
* Tests for {@link SslBuilderCustomizer}
|
||||
@@ -60,15 +60,11 @@ public class SslBuilderCustomizerTests {
|
||||
ssl.setKeyStoreProvider("com.example.KeyStoreProvider");
|
||||
SslBuilderCustomizer customizer = new SslBuilderCustomizer(8080,
|
||||
InetAddress.getLocalHost(), ssl, null);
|
||||
try {
|
||||
ReflectionTestUtils.invokeMethod(customizer, "getKeyManagers", ssl, null);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
Throwable cause = ex.getCause();
|
||||
assertThat(cause).isInstanceOf(NoSuchProviderException.class);
|
||||
assertThat(cause).hasMessageContaining("com.example.KeyStoreProvider");
|
||||
}
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> ReflectionTestUtils.invokeMethod(customizer,
|
||||
"getKeyManagers", ssl, null))
|
||||
.withCauseInstanceOf(NoSuchProviderException.class)
|
||||
.withMessageContaining("com.example.KeyStoreProvider");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -79,15 +75,11 @@ public class SslBuilderCustomizerTests {
|
||||
ssl.setTrustStoreProvider("com.example.TrustStoreProvider");
|
||||
SslBuilderCustomizer customizer = new SslBuilderCustomizer(8080,
|
||||
InetAddress.getLocalHost(), ssl, null);
|
||||
try {
|
||||
ReflectionTestUtils.invokeMethod(customizer, "getTrustManagers", ssl, null);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
Throwable cause = ex.getCause();
|
||||
assertThat(cause).isInstanceOf(NoSuchProviderException.class);
|
||||
assertThat(cause).hasMessageContaining("com.example.TrustStoreProvider");
|
||||
}
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> ReflectionTestUtils.invokeMethod(customizer,
|
||||
"getTrustManagers", ssl, null))
|
||||
.withCauseInstanceOf(NoSuchProviderException.class)
|
||||
.withMessageContaining("com.example.TrustStoreProvider");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -96,15 +88,11 @@ public class SslBuilderCustomizerTests {
|
||||
Ssl ssl = new Ssl();
|
||||
SslBuilderCustomizer customizer = new SslBuilderCustomizer(8080,
|
||||
InetAddress.getLocalHost(), ssl, null);
|
||||
try {
|
||||
ReflectionTestUtils.invokeMethod(customizer, "getKeyManagers", ssl, null);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
Throwable cause = ex.getCause();
|
||||
assertThat(cause).isInstanceOf(WebServerException.class);
|
||||
assertThat(cause).hasMessageContaining("Could not load key store 'null'");
|
||||
}
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> ReflectionTestUtils.invokeMethod(customizer,
|
||||
"getKeyManagers", ssl, null))
|
||||
.withCauseInstanceOf(WebServerException.class)
|
||||
.withMessageContaining("Could not load key store 'null'");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-2019 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.
|
||||
@@ -911,17 +911,13 @@ public abstract class AbstractServletWebServerFactoryTests {
|
||||
public void portClashOfPrimaryConnectorResultsInPortInUseException()
|
||||
throws IOException {
|
||||
doWithBlockedPort((port) -> {
|
||||
try {
|
||||
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> {
|
||||
AbstractServletWebServerFactory factory = getFactory();
|
||||
factory.setPort(port);
|
||||
AbstractServletWebServerFactoryTests.this.webServer = factory
|
||||
.getWebServer();
|
||||
AbstractServletWebServerFactoryTests.this.webServer.start();
|
||||
fail();
|
||||
}
|
||||
catch (RuntimeException ex) {
|
||||
handleExceptionCausedByBlockedPort(ex, port);
|
||||
}
|
||||
}).satisfies((ex) -> handleExceptionCausedByBlockedPort(ex, port));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -929,17 +925,13 @@ public abstract class AbstractServletWebServerFactoryTests {
|
||||
public void portClashOfSecondaryConnectorResultsInPortInUseException()
|
||||
throws IOException {
|
||||
doWithBlockedPort((port) -> {
|
||||
try {
|
||||
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> {
|
||||
AbstractServletWebServerFactory factory = getFactory();
|
||||
addConnector(port, factory);
|
||||
AbstractServletWebServerFactoryTests.this.webServer = factory
|
||||
.getWebServer();
|
||||
AbstractServletWebServerFactoryTests.this.webServer.start();
|
||||
fail();
|
||||
}
|
||||
catch (RuntimeException ex) {
|
||||
handleExceptionCausedByBlockedPort(ex, port);
|
||||
}
|
||||
}).satisfies((ex) -> handleExceptionCausedByBlockedPort(ex, port));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user