Skip to content

Commit 945d3b0

Browse files
committed
DATAMONGO-2321 - Polishing.
Reduce AtTest(expected = …) and ExpectedException with the corresponding AssertJ assertThatExceptionOfType(…) and assertThatIllegalArgumentException().isThrownBy(…).
1 parent fad1834 commit 945d3b0

File tree

81 files changed

+523
-688
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+523
-688
lines changed

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AbstractMongoConfigurationUnitTests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
import java.util.Collections;
2626
import java.util.Set;
2727

28-
import org.junit.Rule;
2928
import org.junit.Test;
30-
import org.junit.rules.ExpectedException;
3129

3230
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3331
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -55,8 +53,6 @@
5553
*/
5654
public class AbstractMongoConfigurationUnitTests {
5755

58-
@Rule public ExpectedException exception = ExpectedException.none();
59-
6056
@Test // DATAMONGO-496
6157
public void usesConfigClassPackageAsBaseMappingPackage() throws ClassNotFoundException {
6258

@@ -84,9 +80,8 @@ public void containsMongoDbFactoryButNoMongoBean() {
8480
AbstractApplicationContext context = new AnnotationConfigApplicationContext(SampleMongoConfiguration.class);
8581

8682
assertThat(context.getBean(MongoDbFactory.class)).isNotNull();
83+
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> context.getBean(MongoClient.class));
8784

88-
exception.expect(NoSuchBeanDefinitionException.class);
89-
context.getBean(MongoClient.class);
9085
context.close();
9186
}
9287

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AbstractReactiveMongoConfigurationUnitTests.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
import java.util.Collections;
2626
import java.util.Set;
2727

28-
import org.junit.Rule;
2928
import org.junit.Test;
30-
import org.junit.rules.ExpectedException;
3129

3230
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3331
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -55,8 +53,6 @@
5553
*/
5654
public class AbstractReactiveMongoConfigurationUnitTests {
5755

58-
@Rule public ExpectedException exception = ExpectedException.none();
59-
6056
@Test // DATAMONGO-1444
6157
public void usesConfigClassPackageAsBaseMappingPackage() throws ClassNotFoundException {
6258

@@ -84,14 +80,9 @@ public void containsMongoDbFactoryButNoMongoBean() {
8480
AbstractApplicationContext context = new AnnotationConfigApplicationContext(SampleMongoConfiguration.class);
8581

8682
assertThat(context.getBean(SimpleReactiveMongoDatabaseFactory.class)).isNotNull();
83+
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> context.getBean(Mongo.class));
8784

88-
exception.expect(NoSuchBeanDefinitionException.class);
89-
90-
try {
91-
context.getBean(Mongo.class);
92-
} finally {
93-
context.close();
94-
}
85+
context.close();
9586
}
9687

9788
@Test // DATAMONGO-1444

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MappingMongoConverterParserIntegrationTests.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
import java.util.Set;
2222

2323
import org.bson.Document;
24-
import org.junit.Rule;
2524
import org.junit.Test;
26-
import org.junit.rules.ExpectedException;
2725

2826
import org.springframework.beans.factory.config.BeanDefinition;
2927
import org.springframework.beans.factory.config.BeanReference;
@@ -53,8 +51,6 @@
5351
*/
5452
public class MappingMongoConverterParserIntegrationTests {
5553

56-
@Rule public ExpectedException exception = ExpectedException.none();
57-
5854
DefaultListableBeanFactory factory;
5955

6056
@Test // DATAMONGO-243
@@ -99,22 +95,20 @@ public void activatesAbbreviatingPropertiesCorrectly() {
9995
@Test // DATAMONGO-866
10096
public void rejectsInvalidFieldNamingStrategyConfiguration() {
10197

102-
exception.expect(BeanDefinitionParsingException.class);
103-
exception.expectMessage("abbreviation");
104-
exception.expectMessage("field-naming-strategy-ref");
105-
10698
BeanDefinitionRegistry factory = new DefaultListableBeanFactory();
10799
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
108-
reader.loadBeanDefinitions(new ClassPathResource("namespace/converter-invalid.xml"));
100+
101+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
102+
.isThrownBy(() -> reader.loadBeanDefinitions(new ClassPathResource("namespace/converter-invalid.xml")))
103+
.withMessageContaining("abbreviation").withMessageContaining("field-naming-strategy-ref");
109104
}
110105

111106
@Test // DATAMONGO-892
112107
public void shouldThrowBeanDefinitionParsingExceptionIfConverterDefinedAsNestedBean() {
113108

114-
exception.expect(BeanDefinitionParsingException.class);
115-
exception.expectMessage("Mongo Converter must not be defined as nested bean.");
109+
assertThatExceptionOfType(BeanDefinitionParsingException.class).isThrownBy(this::loadNestedBeanConfiguration)
110+
.withMessageContaining("Mongo Converter must not be defined as nested bean.");
116111

117-
loadNestedBeanConfiguration();
118112
}
119113

120114
@Test // DATAMONGO-925, DATAMONGO-928

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoAuditingRegistrarUnitTests.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@
1515
*/
1616
package org.springframework.data.mongodb.config;
1717

18+
import static org.assertj.core.api.Assertions.*;
19+
1820
import org.junit.Test;
1921
import org.junit.runner.RunWith;
2022
import org.mockito.Mock;
2123
import org.mockito.junit.MockitoJUnitRunner;
24+
2225
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2326
import org.springframework.core.type.AnnotationMetadata;
2427

2528
/**
26-
* Unit tests for {@link JpaAuditingRegistrar}.
29+
* Unit tests for {@link MongoAuditingRegistrar}.
2730
*
2831
* @author Oliver Gierke
2932
*/
@@ -35,13 +38,13 @@ public class MongoAuditingRegistrarUnitTests {
3538
@Mock AnnotationMetadata metadata;
3639
@Mock BeanDefinitionRegistry registry;
3740

38-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-792
41+
@Test // DATAMONGO-792
3942
public void rejectsNullAnnotationMetadata() {
40-
registrar.registerBeanDefinitions(null, registry);
43+
assertThatIllegalArgumentException().isThrownBy(() -> registrar.registerBeanDefinitions(null, registry));
4144
}
4245

43-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-792
46+
@Test // DATAMONGO-792
4447
public void rejectsNullBeanDefinitionRegistry() {
45-
registrar.registerBeanDefinitions(metadata, null);
48+
assertThatIllegalArgumentException().isThrownBy(() -> registrar.registerBeanDefinitions(metadata, null));
4649
}
4750
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditorUnitTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,15 @@ public void shouldReturnNullValueForEmptyText() {
137137
assertThat(getValue()).isNull();
138138
}
139139

140-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1158
140+
@Test // DATAMONGO-1158
141141
public void shouldThrowExceptionForMalformatedCredentialsString() {
142-
editor.setAsText("tyrion");
142+
assertThatIllegalArgumentException().isThrownBy(() -> editor.setAsText("tyrion"));
143143
}
144144

145-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1158
145+
@Test // DATAMONGO-1158
146146
public void shouldThrowExceptionForMalformatedAuthMechanism() {
147-
editor.setAsText(USER_2_AUTH_STRING + "?uri.authMechanism=Targaryen");
147+
assertThatIllegalArgumentException()
148+
.isThrownBy(() -> editor.setAsText(USER_2_AUTH_STRING + "?uri.authMechanism=Targaryen"));
148149
}
149150

150151
@Test // DATAMONGO-1158
@@ -283,10 +284,10 @@ public void shouldReturnScramSha256Credentials() {
283284
assertThat(getValue()).contains(SCRAM_SHA_256_CREDENTIALS);
284285
}
285286

286-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-2016
287+
@Test // DATAMONGO-2016
287288
@SuppressWarnings("unchecked")
288289
public void failsGracefullyOnEmptyQueryArgument() {
289-
editor.setAsText(USER_5_AUTH_STRING_WITH_QUERY_ARGS);
290+
assertThatIllegalArgumentException().isThrownBy(() -> editor.setAsText(USER_5_AUTH_STRING_WITH_QUERY_ARGS));
290291
}
291292

292293
@SuppressWarnings("unchecked")

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoDbFactoryNoDatabaseRunningTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ public void startsUpWithoutADatabaseRunning() {
4444
assertThat(mongoTemplate.getClass().getName()).isEqualTo("org.springframework.data.mongodb.core.MongoTemplate");
4545
}
4646

47-
@Test(expected = DataAccessResourceFailureException.class)
47+
@Test
4848
public void failsDataAccessWithoutADatabaseRunning() {
49-
mongoTemplate.getCollectionNames();
49+
assertThatExceptionOfType(DataAccessResourceFailureException.class)
50+
.isThrownBy(() -> mongoTemplate.getCollectionNames());
5051
}
5152
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/ReadPreferencePropertyEditorUnitTests.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import org.junit.Before;
21-
import org.junit.Rule;
2221
import org.junit.Test;
23-
import org.junit.rules.ExpectedException;
2422

2523
import com.mongodb.ReadPreference;
2624

@@ -31,8 +29,6 @@
3129
*/
3230
public class ReadPreferencePropertyEditorUnitTests {
3331

34-
@Rule public ExpectedException expectedException = ExpectedException.none();
35-
3632
ReadPreferencePropertyEditor editor;
3733

3834
@Before
@@ -43,11 +39,8 @@ public void setUp() {
4339
@Test // DATAMONGO-1158
4440
public void shouldThrowExceptionOnUndefinedPreferenceString() {
4541

46-
expectedException.expect(IllegalArgumentException.class);
47-
expectedException.expectMessage("ReadPreference");
48-
expectedException.expectMessage("foo");
49-
50-
editor.setAsText("foo");
42+
assertThatIllegalArgumentException().isThrownBy(() -> editor.setAsText("foo")).withMessageContaining("foo")
43+
.withMessageContaining("ReadPreference");
5144
}
5245

5346
@Test // DATAMONGO-1158

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/ServerAddressPropertyEditorUnitTests.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
import java.util.Collection;
2525

2626
import org.junit.Before;
27-
import org.junit.Rule;
2827
import org.junit.Test;
29-
import org.junit.rules.ExpectedException;
3028

3129
import com.mongodb.ServerAddress;
3230

@@ -38,8 +36,6 @@
3836
*/
3937
public class ServerAddressPropertyEditorUnitTests {
4038

41-
@Rule public ExpectedException expectedException = ExpectedException.none();
42-
4339
ServerAddressPropertyEditor editor;
4440

4541
@Before
@@ -123,13 +119,11 @@ public void handleIPv6HostaddressLoopbackLongWithBrackets() throws UnknownHostEx
123119
* We can't tell whether the last part of the hostAddress represents a port or not.
124120
*/
125121
@Test // DATAMONGO-808
126-
public void shouldFailToHandleAmbiguousIPv6HostaddressLongWithoutPortAndWithoutBrackets()
127-
throws UnknownHostException {
128-
129-
expectedException.expect(IllegalArgumentException.class);
122+
public void shouldFailToHandleAmbiguousIPv6HostaddressLongWithoutPortAndWithoutBrackets() {
130123

131124
String hostAddress = "0000:0000:0000:0000:0000:0000:0000:128";
132-
editor.setAsText(hostAddress);
125+
126+
assertThatIllegalArgumentException().isThrownBy(() -> editor.setAsText(hostAddress));
133127
}
134128

135129
@Test // DATAMONGO-808

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultBulkOperationsIntegrationTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,22 @@ public void setUp() {
6969
this.collection.deleteMany(new Document());
7070
}
7171

72-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-934
72+
@Test // DATAMONGO-934
7373
public void rejectsNullMongoOperations() {
74-
new DefaultBulkOperations(null, COLLECTION_NAME,
75-
new BulkOperationContext(BulkMode.ORDERED, Optional.empty(), null, null, null, null));
76-
74+
assertThatIllegalArgumentException().isThrownBy(() -> new DefaultBulkOperations(null, COLLECTION_NAME,
75+
new BulkOperationContext(BulkMode.ORDERED, Optional.empty(), null, null, null, null)));
7776
}
7877

79-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-934
78+
@Test // DATAMONGO-934
8079
public void rejectsNullCollectionName() {
81-
new DefaultBulkOperations(operations, null,
82-
new BulkOperationContext(BulkMode.ORDERED, Optional.empty(), null, null, null, null));
80+
assertThatIllegalArgumentException().isThrownBy(() -> new DefaultBulkOperations(operations, null,
81+
new BulkOperationContext(BulkMode.ORDERED, Optional.empty(), null, null, null, null)));
8382
}
8483

85-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-934
84+
@Test // DATAMONGO-934
8685
public void rejectsEmptyCollectionName() {
87-
new DefaultBulkOperations(operations, "", new BulkOperationContext(BulkMode.ORDERED, Optional.empty(), null, null, null, null));
86+
assertThatIllegalArgumentException().isThrownBy(() -> new DefaultBulkOperations(operations, "",
87+
new BulkOperationContext(BulkMode.ORDERED, Optional.empty(), null, null, null, null)));
8888
}
8989

9090
@Test // DATAMONGO-934

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultScriptOperationsTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ public void callShouldExecuteExistingScript() {
146146
assertThat(result).isEqualTo((Object) 10D);
147147
}
148148

149-
@Test(expected = UncategorizedDataAccessException.class) // DATAMONGO-479
149+
@Test // DATAMONGO-479
150150
public void callShouldThrowExceptionWhenCallingScriptThatDoesNotExist() {
151-
scriptOps.call(SCRIPT_NAME, 10);
151+
assertThatExceptionOfType(UncategorizedDataAccessException.class).isThrownBy(() -> scriptOps.call(SCRIPT_NAME, 10));
152152
}
153153

154154
@Test // DATAMONGO-479

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultScriptOperationsUnitTests.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ public void setUp() {
4646
this.scriptOps = new DefaultScriptOperations(mongoOperations);
4747
}
4848

49-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-479
49+
@Test // DATAMONGO-479
5050
public void rejectsNullExecutableMongoScript() {
51-
scriptOps.register((ExecutableMongoScript) null);
51+
assertThatIllegalArgumentException().isThrownBy(() -> scriptOps.register((ExecutableMongoScript) null));
5252
}
5353

54-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-479
54+
@Test // DATAMONGO-479
5555
public void rejectsNullNamedMongoScript() {
56-
scriptOps.register((NamedMongoScript) null);
56+
assertThatIllegalArgumentException().isThrownBy(() -> scriptOps.register((NamedMongoScript) null));
5757
}
5858

5959
@Test // DATAMONGO-479
@@ -75,28 +75,28 @@ public void saveShouldGenerateScriptNameForExecutableMongoScripts() {
7575
assertThat(captor.getValue().getName()).isNotNull();
7676
}
7777

78-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-479
78+
@Test // DATAMONGO-479
7979
public void executeShouldThrowExceptionWhenScriptIsNull() {
80-
scriptOps.execute(null);
80+
assertThatIllegalArgumentException().isThrownBy(() -> scriptOps.execute(null));
8181
}
8282

83-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-479
83+
@Test // DATAMONGO-479
8484
public void existsShouldThrowExceptionWhenScriptNameIsNull() {
85-
scriptOps.exists(null);
85+
assertThatIllegalArgumentException().isThrownBy(() -> scriptOps.exists(null));
8686
}
8787

88-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-479
88+
@Test // DATAMONGO-479
8989
public void existsShouldThrowExceptionWhenScriptNameIsEmpty() {
90-
scriptOps.exists("");
90+
assertThatIllegalArgumentException().isThrownBy(() -> scriptOps.exists(""));
9191
}
9292

93-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-479
93+
@Test // DATAMONGO-479
9494
public void callShouldThrowExceptionWhenScriptNameIsNull() {
95-
scriptOps.call(null);
95+
assertThatIllegalArgumentException().isThrownBy(() -> scriptOps.call(null));
9696
}
9797

98-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-479
98+
@Test // DATAMONGO-479
9999
public void callShouldThrowExceptionWhenScriptNameIsEmpty() {
100-
scriptOps.call("");
100+
assertThatIllegalArgumentException().isThrownBy(() -> scriptOps.call(""));
101101
}
102102
}

0 commit comments

Comments
 (0)