Skip to content

Commit a569f36

Browse files
committed
Merge pull request #16066 from dreis2211
* pr/16066: Polish "Use AssertJ facilities for expected exceptions" Use AssertJ facilities for expected exceptions
2 parents 4fec6b9 + 235fbfb commit a569f36

File tree

11 files changed

+62
-39
lines changed

11 files changed

+62
-39
lines changed

spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngineTests.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@
3636
import org.springframework.test.util.ReflectionTestUtils;
3737

3838
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3940

4041
/**
4142
* Tests for {@link AetherGrapeEngine}.
@@ -147,13 +148,15 @@ public void resolutionWithCustomResolver() {
147148
assertThat(this.groovyClassLoader.getURLs().length).isEqualTo(1);
148149
}
149150

150-
@Test(expected = IllegalArgumentException.class)
151+
@Test
151152
public void differingTypeAndExt() {
152153
Map<String, Object> dependency = createDependency("org.grails",
153154
"grails-dependencies", "2.4.0");
154155
dependency.put("type", "foo");
155156
dependency.put("ext", "bar");
156-
createGrapeEngine().grab(Collections.emptyMap(), dependency);
157+
AetherGrapeEngine grapeEngine = createGrapeEngine();
158+
assertThatIllegalArgumentException()
159+
.isThrownBy(() -> grapeEngine.grab(Collections.emptyMap(), dependency));
157160
}
158161

159162
@Test

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/AutoConfigureMockRestServiceServerEnabledFalseIntegrationTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,8 @@
2525
import org.springframework.context.ApplicationContext;
2626
import org.springframework.test.context.junit4.SpringRunner;
2727

28+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
29+
2830
/**
2931
* Tests for {@link AutoConfigureMockRestServiceServer} with {@code enabled=false}.
3032
*
@@ -38,9 +40,11 @@ public class AutoConfigureMockRestServiceServerEnabledFalseIntegrationTests {
3840
@Autowired
3941
private ApplicationContext applicationContext;
4042

41-
@Test(expected = NoSuchBeanDefinitionException.class)
43+
@Test
4244
public void mockServerRestTemplateCustomizerShouldNotBeRegistered() {
43-
this.applicationContext.getBean(MockServerRestTemplateCustomizer.class);
45+
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
46+
.isThrownBy(() -> this.applicationContext
47+
.getBean(MockServerRestTemplateCustomizer.class));
4448
}
4549

4650
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestNoComponentIntegrationTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.springframework.test.web.client.MockRestServiceServer;
2929

3030
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3132
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
3233
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
3334

@@ -49,9 +50,10 @@ public class RestClientTestNoComponentIntegrationTests {
4950
@Autowired
5051
private MockRestServiceServer server;
5152

52-
@Test(expected = NoSuchBeanDefinitionException.class)
53+
@Test
5354
public void exampleRestClientIsNotInjected() {
54-
this.applicationContext.getBean(ExampleRestClient.class);
55+
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(
56+
() -> this.applicationContext.getBean(ExampleRestClient.class));
5557
}
5658

5759
@Test

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SimpleMainTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
import org.springframework.util.StringUtils;
3030

3131
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3233

3334
/**
3435
* Tests for {@link SpringApplication} main method.
@@ -43,10 +44,10 @@ public class SimpleMainTests {
4344

4445
private static final String SPRING_STARTUP = "Started SpringApplication in";
4546

46-
@Test(expected = IllegalArgumentException.class)
47+
@Test
4748
public void emptyApplicationContext() throws Exception {
48-
SpringApplication.main(getArgs());
49-
assertThat(getOutput()).contains(SPRING_STARTUP);
49+
assertThatIllegalArgumentException()
50+
.isThrownBy(() -> SpringApplication.main(getArgs()));
5051
}
5152

5253
@Test

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/FileEncodingApplicationListenerTests.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.context;
1818

19-
import org.junit.Assume;
2019
import org.junit.Test;
2120

2221
import org.springframework.boot.SpringApplication;
@@ -26,6 +25,9 @@
2625
import org.springframework.core.env.StandardEnvironment;
2726
import org.springframework.test.context.support.TestPropertySourceUtils;
2827

28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
30+
2931
/**
3032
* Tests for {@link FileEncodingApplicationListener}.
3133
*
@@ -40,12 +42,13 @@ public class FileEncodingApplicationListenerTests {
4042
private final ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent(
4143
new SpringApplication(), new String[0], this.environment);
4244

43-
@Test(expected = IllegalStateException.class)
45+
@Test
4446
public void testIllegalState() {
4547
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
4648
"spring.mandatory_file_encoding=FOO");
4749
ConfigurationPropertySources.attach(this.environment);
48-
this.initializer.onApplicationEvent(this.event);
50+
assertThatIllegalStateException()
51+
.isThrownBy(() -> this.initializer.onApplicationEvent(this.event));
4952
}
5053

5154
@Test
@@ -55,7 +58,7 @@ public void testSunnyDayNothingMandated() {
5558

5659
@Test
5760
public void testSunnyDayMandated() {
58-
Assume.assumeNotNull(System.getProperty("file.encoding"));
61+
assertThat(System.getProperty("file.encoding")).isNotNull();
5962
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
6063
"spring.mandatory_file_encoding:" + System.getProperty("file.encoding"));
6164
ConfigurationPropertySources.attach(this.environment);

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemTests.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import org.springframework.boot.logging.LoggingSystem.NoOpLoggingSystem;
2323

2424
import static org.assertj.core.api.Assertions.assertThat;
25+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2526

2627
/**
2728
* Tests for {@link LoggingSystem}.
@@ -42,14 +43,16 @@ public void loggingSystemCanBeDisabled() {
4243
assertThat(loggingSystem).isInstanceOf(NoOpLoggingSystem.class);
4344
}
4445

45-
@Test(expected = UnsupportedOperationException.class)
46+
@Test
4647
public void getLoggerConfigurationIsUnsupported() {
47-
new StubLoggingSystem().getLoggerConfiguration("test-logger-name");
48+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(
49+
() -> new StubLoggingSystem().getLoggerConfiguration("test-logger-name"));
4850
}
4951

50-
@Test(expected = UnsupportedOperationException.class)
52+
@Test
5153
public void listLoggerConfigurationsIsUnsupported() {
52-
new StubLoggingSystem().getLoggerConfigurations();
54+
assertThatExceptionOfType(UnsupportedOperationException.class)
55+
.isThrownBy(() -> new StubLoggingSystem().getLoggerConfigurations());
5356
}
5457

5558
private static final class StubLoggingSystem extends LoggingSystem {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/java/JavaLoggingSystemTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@
3939
import org.springframework.util.StringUtils;
4040

4141
import static org.assertj.core.api.Assertions.assertThat;
42+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
4243

4344
/**
4445
* Tests for {@link JavaLoggingSystem}.
@@ -137,11 +138,11 @@ public void testNonDefaultConfigLocation() {
137138
assertThat(output).contains("INFO: Hello");
138139
}
139140

140-
@Test(expected = IllegalStateException.class)
141+
@Test
141142
public void testNonexistentConfigLocation() {
142143
this.loggingSystem.beforeInitialize();
143-
this.loggingSystem.initialize(null, "classpath:logging-nonexistent.properties",
144-
null);
144+
assertThatIllegalStateException().isThrownBy(() -> this.loggingSystem
145+
.initialize(null, "classpath:logging-nonexistent.properties", null));
145146
}
146147

147148
@Test

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,6 +48,7 @@
4848
import org.springframework.util.StringUtils;
4949

5050
import static org.assertj.core.api.Assertions.assertThat;
51+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
5152
import static org.hamcrest.Matchers.containsString;
5253
import static org.hamcrest.Matchers.not;
5354
import static org.mockito.ArgumentMatchers.any;
@@ -126,10 +127,11 @@ public void testNonDefaultConfigLocation() {
126127
assertThat(configuration.getWatchManager().getIntervalSeconds()).isEqualTo(30);
127128
}
128129

129-
@Test(expected = IllegalStateException.class)
130+
@Test
130131
public void testNonexistentConfigLocation() {
131132
this.loggingSystem.beforeInitialize();
132-
this.loggingSystem.initialize(null, "classpath:log4j2-nonexistent.xml", null);
133+
assertThatIllegalStateException().isThrownBy(() -> this.loggingSystem
134+
.initialize(null, "classpath:log4j2-nonexistent.xml", null));
133135
}
134136

135137
@Test

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,6 +61,7 @@
6161
import org.springframework.util.StringUtils;
6262

6363
import static org.assertj.core.api.Assertions.assertThat;
64+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
6465
import static org.hamcrest.Matchers.containsString;
6566
import static org.hamcrest.Matchers.not;
6667
import static org.mockito.Mockito.mock;
@@ -175,11 +176,11 @@ public void testLogbackSpecificSystemProperty() {
175176
}
176177
}
177178

178-
@Test(expected = IllegalStateException.class)
179+
@Test
179180
public void testNonexistentConfigLocation() {
180181
this.loggingSystem.beforeInitialize();
181-
this.loggingSystem.initialize(this.initializationContext,
182-
"classpath:logback-nonexistent.xml", null);
182+
assertThatIllegalStateException().isThrownBy(() -> this.loggingSystem.initialize(
183+
this.initializationContext, "classpath:logback-nonexistent.xml", null));
183184
}
184185

185186
@Test

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/server/MimeMappingsTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import org.junit.Test;
2525

2626
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2728

2829
/**
2930
* Tests for {@link MimeMappings}.
@@ -32,9 +33,10 @@
3233
*/
3334
public class MimeMappingsTests {
3435

35-
@Test(expected = UnsupportedOperationException.class)
36+
@Test
3637
public void defaultsCannotBeModified() {
37-
MimeMappings.DEFAULT.add("foo", "foo/bar");
38+
assertThatExceptionOfType(UnsupportedOperationException.class)
39+
.isThrownBy(() -> MimeMappings.DEFAULT.add("foo", "foo/bar"));
3840
}
3941

4042
@Test

0 commit comments

Comments
 (0)