Skip to content

Commit 704913c

Browse files
committed
DATACMNS-1554 - Polishing.
Replace AtTest(expected = …) and ExpectedException with the corresponding AssertJ assertThatExceptionOfType(…) and assertThatIllegalArgumentException().isThrownBy(…).
1 parent 660006b commit 704913c

File tree

79 files changed

+457
-474
lines changed

Some content is hidden

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

79 files changed

+457
-474
lines changed

src/test/java/org/springframework/data/auditing/AnnotationAuditingMetadataUnitTests.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919

2020
import java.lang.reflect.Field;
2121

22-
import org.junit.Rule;
2322
import org.junit.Test;
24-
import org.junit.rules.ExpectedException;
23+
2524
import org.springframework.data.annotation.CreatedDate;
2625
import org.springframework.util.ReflectionUtils;
2726

@@ -39,8 +38,6 @@ public class AnnotationAuditingMetadataUnitTests {
3938
static final Field lastModifiedByField = ReflectionUtils.findField(AnnotatedUser.class, "lastModifiedBy");
4039
static final Field lastModifiedDateField = ReflectionUtils.findField(AnnotatedUser.class, "lastModifiedDate");
4140

42-
@Rule public ExpectedException exception = ExpectedException.none();
43-
4441
@Test
4542
public void checkAnnotationDiscovery() {
4643

@@ -82,11 +79,8 @@ class Sample {
8279
@CreatedDate String field;
8380
}
8481

85-
exception.expect(IllegalStateException.class);
86-
exception.expectMessage(String.class.getName());
87-
exception.expectMessage("field");
88-
89-
AnnotationAuditingMetadata.getMetadata(Sample.class);
82+
assertThatIllegalStateException().isThrownBy(() -> AnnotationAuditingMetadata.getMetadata(Sample.class))
83+
.withMessageContaining("field").withMessageContaining("String");
9084
}
9185

9286
@SuppressWarnings("unused")

src/test/java/org/springframework/data/auditing/DefaultAuditableBeanWrapperFactoryUnitTests.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Optional;
2727

2828
import org.junit.Test;
29+
2930
import org.springframework.data.annotation.CreatedDate;
3031
import org.springframework.data.annotation.LastModifiedDate;
3132
import org.springframework.data.auditing.DefaultAuditableBeanWrapperFactory.AuditableInterfaceBeanWrapper;
@@ -43,9 +44,9 @@ public class DefaultAuditableBeanWrapperFactoryUnitTests {
4344

4445
DefaultAuditableBeanWrapperFactory factory = new DefaultAuditableBeanWrapperFactory();
4546

46-
@Test(expected = IllegalArgumentException.class)
47+
@Test
4748
public void rejectsNullSource() {
48-
factory.getBeanWrapperFor(null);
49+
assertThatIllegalArgumentException().isThrownBy(() -> factory.getBeanWrapperFor(null));
4950
}
5051

5152
@Test
@@ -85,15 +86,18 @@ public void setsJsr310AndThreeTenBpTypes() {
8586
});
8687
}
8788

88-
@Test(expected = IllegalArgumentException.class) // DATACMNS-867
89+
@Test // DATACMNS-867
8990
public void errorsWhenUnableToConvertDateViaIntermediateJavaUtilDateConversion() {
9091

9192
Jsr310ThreeTenBpAuditedUser user = new Jsr310ThreeTenBpAuditedUser();
9293
ZonedDateTime zonedDateTime = ZonedDateTime.now();
9394

9495
Optional<AuditableBeanWrapper<Jsr310ThreeTenBpAuditedUser>> wrapper = factory.getBeanWrapperFor(user);
9596

96-
assertThat(wrapper).hasValueSatisfying(it -> it.setLastModifiedDate(zonedDateTime));
97+
assertThat(wrapper).isNotEmpty();
98+
99+
assertThatExceptionOfType(IllegalArgumentException.class)
100+
.isThrownBy(() -> wrapper.ifPresent(it -> it.setLastModifiedDate(zonedDateTime)));
97101
}
98102

99103
@Test // DATACMNS-1259

src/test/java/org/springframework/data/auditing/IsNewAwareAuditingHandlerUnitTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.junit.Test;
2424
import org.junit.runner.RunWith;
2525
import org.mockito.junit.MockitoJUnitRunner;
26+
2627
import org.springframework.data.annotation.Id;
2728
import org.springframework.data.mapping.context.PersistentEntities;
2829
import org.springframework.data.mapping.context.SampleMappingContext;
@@ -74,9 +75,9 @@ public void delegatesToMarkModifiedForNonNewEntity() {
7475
assertThat(user.modifiedDate).isNotNull();
7576
}
7677

77-
@Test(expected = IllegalArgumentException.class) // DATACMNS-365
78+
@Test // DATACMNS-365
7879
public void rejectsNullMappingContext() {
79-
new IsNewAwareAuditingHandler((PersistentEntities) null);
80+
assertThatIllegalArgumentException().isThrownBy(() -> new IsNewAwareAuditingHandler((PersistentEntities) null));
8081
}
8182

8283
@Test // DATACMNS-365

src/test/java/org/springframework/data/convert/ClassGeneratingEntityInstantiatorUnitTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ public void instantiatesTypeWithPreferredConstructorUsingParameterValueProvider(
8686
.satisfies(it -> verify(provider, times(1)).getParameterValue(it.getParameters().iterator().next()));
8787
}
8888

89-
@Test(expected = MappingInstantiationException.class) // DATACMNS-300, DATACMNS-578
89+
@Test // DATACMNS-300, DATACMNS-578
9090
@SuppressWarnings({ "unchecked", "rawtypes" })
9191
public void throwsExceptionOnBeanInstantiationException() {
9292

9393
doReturn(PersistentEntity.class).when(entity).getType();
9494

95-
this.instance.createInstance(entity, provider);
95+
assertThatExceptionOfType(MappingInstantiationException.class)
96+
.isThrownBy(() -> this.instance.createInstance(entity, provider));
9697
}
9798

9899
@Test // DATACMNS-134, DATACMNS-578

src/test/java/org/springframework/data/convert/ConfigurableTypeInformationMapperUnitTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ public void setUp() {
4545
mapper = new ConfigurableTypeInformationMapper(Collections.singletonMap(String.class, "1"));
4646
}
4747

48-
@Test(expected = IllegalArgumentException.class)
48+
@Test
4949
public void rejectsNullTypeMap() {
50-
new ConfigurableTypeInformationMapper(null);
50+
assertThatIllegalArgumentException().isThrownBy(() -> new ConfigurableTypeInformationMapper(null));
5151
}
5252

53-
@Test(expected = IllegalArgumentException.class)
53+
@Test
5454
public void rejectsNonBijectionalMap() {
5555

5656
Map<Class<?>, String> map = new HashMap<>();
5757
map.put(String.class, "1");
5858
map.put(Object.class, "1");
5959

60-
new ConfigurableTypeInformationMapper(map);
60+
assertThatIllegalArgumentException().isThrownBy(() -> new ConfigurableTypeInformationMapper(map));
6161
}
6262

6363
@Test

src/test/java/org/springframework/data/convert/EntityInstantiatorsUnitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public class EntityInstantiatorsUnitTests {
3838
@Mock PersistentEntity<?, ?> entity;
3939
@Mock EntityInstantiator customInstantiator;
4040

41-
@Test(expected = IllegalArgumentException.class)
41+
@Test
4242
public void rejectsNullFallbackInstantiator() {
43-
new EntityInstantiators((EntityInstantiator) null);
43+
assertThatIllegalArgumentException().isThrownBy(() -> new EntityInstantiators((EntityInstantiator) null));
4444
}
4545

4646
@Test

src/test/java/org/springframework/data/convert/MappingContextTypeInformationMapperUnitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public void setUp() {
4545
mappingContext = new SampleMappingContext();
4646
}
4747

48-
@Test(expected = IllegalArgumentException.class)
48+
@Test
4949
public void rejectsNullMappingContext() {
50-
new MappingContextTypeInformationMapper(null);
50+
assertThatIllegalArgumentException().isThrownBy(() -> new MappingContextTypeInformationMapper(null));
5151
}
5252

5353
@Test

src/test/java/org/springframework/data/convert/ReflectionEntityInstantiatorUnitTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@ public void instantiatesTypeWithPreferredConstructorUsingParameterValueProvider(
8080
.satisfies(it -> verify(provider, times(1)).getParameterValue(it.getParameters().iterator().next()));
8181
}
8282

83-
@Test(expected = MappingInstantiationException.class) // DATACMNS-300
83+
@Test // DATACMNS-300
8484
@SuppressWarnings({ "unchecked", "rawtypes" })
8585
public void throwsExceptionOnBeanInstantiationException() {
8686

8787
doReturn(PersistentEntity.class).when(entity).getType();
8888

89-
INSTANCE.createInstance(entity, provider);
89+
assertThatExceptionOfType(MappingInstantiationException.class)
90+
.isThrownBy(() -> INSTANCE.createInstance(entity, provider));
9091
}
9192

9293
@Test // DATACMNS-134

src/test/java/org/springframework/data/domain/AbstractPageRequestUnitTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public abstract class AbstractPageRequestUnitTests {
2828

2929
public abstract AbstractPageRequest newPageRequest(int page, int size);
3030

31-
@Test(expected = IllegalArgumentException.class) // DATACMNS-402
31+
@Test // DATACMNS-402
3232
public void preventsNegativePage() {
33-
newPageRequest(-1, 10);
33+
assertThatIllegalArgumentException().isThrownBy(() -> newPageRequest(-1, 10));
3434
}
3535

36-
@Test(expected = IllegalArgumentException.class) // DATACMNS-402
36+
@Test // DATACMNS-402
3737
public void preventsNegativeSize() {
38-
newPageRequest(0, -1);
38+
assertThatIllegalArgumentException().isThrownBy(() -> newPageRequest(0, -1));
3939
}
4040

4141
@Test // DATACMNS-402
@@ -72,9 +72,9 @@ public void equalsHonoursPageAndSize() {
7272
assertNotEqualsAndHashcode(request, newPageRequest(0, 11));
7373
}
7474

75-
@Test(expected = IllegalArgumentException.class) // DATACMNS-377
75+
@Test // DATACMNS-377
7676
public void preventsPageSizeLessThanOne() {
77-
newPageRequest(0, 0);
77+
assertThatIllegalArgumentException().isThrownBy(() -> newPageRequest(0, 0));
7878
}
7979

8080
@Test // DATACMNS-1327

src/test/java/org/springframework/data/domain/DirectionUnitTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
/**
99
* Unit test for {@link Direction}.
10-
*
10+
*
1111
* @author Oliver Gierke
1212
*/
1313
public class DirectionUnitTests {
@@ -19,8 +19,8 @@ public void jpaValueMapping() throws Exception {
1919
assertThat(Direction.fromString("desc")).isEqualTo(Direction.DESC);
2020
}
2121

22-
@Test(expected = IllegalArgumentException.class)
23-
public void rejectsInvalidString() throws Exception {
24-
Direction.fromString("foo");
22+
@Test
23+
public void rejectsInvalidString() {
24+
assertThatIllegalArgumentException().isThrownBy(() -> Direction.fromString("foo"));
2525
}
2626
}

src/test/java/org/springframework/data/domain/ExampleMatcherUnitTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ public void nullHandlerShouldReturnIgnoreByDefault() {
5757
assertThat(matcher.getNullHandler()).isEqualTo(NullHandler.IGNORE);
5858
}
5959

60-
@Test(expected = UnsupportedOperationException.class) // DATACMNS-810
60+
@Test // DATACMNS-810
6161
public void ignoredPathsIsNotModifiable() {
62-
matcher.getIgnoredPaths().add(\\_(ツ)_/¯");
62+
assertThatExceptionOfType(UnsupportedOperationException.class)
63+
.isThrownBy(() -> matcher.getIgnoredPaths().add(\\_(ツ)_/¯"));
6364
}
6465

6566
@Test // DATACMNS-810

src/test/java/org/springframework/data/domain/ExampleUnitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public void setUp() {
4242
example = Example.of(person);
4343
}
4444

45-
@Test(expected = IllegalArgumentException.class) // DATACMNS-810
45+
@Test // DATACMNS-810
4646
public void rejectsNullProbe() {
47-
Example.of(null);
47+
assertThatIllegalArgumentException().isThrownBy(() -> Example.of(null));
4848
}
4949

5050
@Test // DATACMNS-810

src/test/java/org/springframework/data/domain/PageImplUnitTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public void assertEqualsForComplexSetup() throws Exception {
5555
assertNotEqualsAndHashcode(page, new PageImpl<>(content, PageRequest.of(0, 15), 100));
5656
}
5757

58-
@Test(expected = IllegalArgumentException.class)
59-
public void preventsNullContentForSimpleSetup() throws Exception {
60-
new PageImpl<>(null);
58+
@Test
59+
public void preventsNullContentForSimpleSetup() {
60+
assertThatIllegalArgumentException().isThrownBy(() -> new PageImpl<>(null));
6161
}
6262

63-
@Test(expected = IllegalArgumentException.class)
64-
public void preventsNullContentForAdvancedSetup() throws Exception {
65-
new PageImpl<>(null, null, 0);
63+
@Test
64+
public void preventsNullContentForAdvancedSetup() {
65+
assertThatIllegalArgumentException().isThrownBy(() -> new PageImpl<>(null, null, 0));
6666
}
6767

6868
@Test

src/test/java/org/springframework/data/domain/RangeUnitTests.java

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

2020
import org.junit.Test;
21+
2122
import org.springframework.data.domain.Range.Bound;
2223

2324
/**
@@ -29,9 +30,10 @@
2930
*/
3031
public class RangeUnitTests {
3132

32-
@Test(expected = IllegalArgumentException.class) // DATACMNS-651
33+
@Test // DATACMNS-651
3334
public void rejectsNullReferenceValuesForContains() {
34-
Range.from(Bound.inclusive(10L)).to(Bound.inclusive(20L)).contains(null);
35+
assertThatIllegalArgumentException()
36+
.isThrownBy(() -> Range.from(Bound.inclusive(10L)).to(Bound.inclusive(20L)).contains(null));
3537
}
3638

3739
@Test // DATACMNS-651

src/test/java/org/springframework/data/domain/SortUnitTests.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class SortUnitTests {
4242
* @throws Exception
4343
*/
4444
@Test
45-
public void appliesDefaultForOrder() throws Exception {
45+
public void appliesDefaultForOrder() {
4646
assertThat(Sort.by("foo").iterator().next().getDirection()).isEqualTo(Sort.DEFAULT_DIRECTION);
4747
}
4848

@@ -51,44 +51,40 @@ public void appliesDefaultForOrder() throws Exception {
5151
*
5252
* @throws Exception
5353
*/
54+
@Test
5455
@SuppressWarnings("null")
55-
@Test(expected = IllegalArgumentException.class)
56-
public void preventsNullProperties() throws Exception {
57-
58-
Sort.by(Direction.ASC, (String[]) null);
56+
public void preventsNullProperties() {
57+
assertThatIllegalArgumentException().isThrownBy(() -> Sort.by(Direction.ASC, (String[]) null));
5958
}
6059

6160
/**
6261
* Asserts that the class rejects {@code null} values in the properties array.
6362
*
6463
* @throws Exception
6564
*/
66-
@Test(expected = IllegalArgumentException.class)
67-
public void preventsNullProperty() throws Exception {
68-
69-
Sort.by(Direction.ASC, (String) null);
65+
@Test
66+
public void preventsNullProperty() {
67+
assertThatIllegalArgumentException().isThrownBy(() -> Sort.by(Direction.ASC, (String) null));
7068
}
7169

7270
/**
7371
* Asserts that the class rejects empty strings in the properties array.
7472
*
7573
* @throws Exception
7674
*/
77-
@Test(expected = IllegalArgumentException.class)
78-
public void preventsEmptyProperty() throws Exception {
79-
80-
Sort.by(Direction.ASC, "");
75+
@Test
76+
public void preventsEmptyProperty() {
77+
assertThatIllegalArgumentException().isThrownBy(() -> Sort.by(Direction.ASC, ""));
8178
}
8279

8380
/**
8481
* Asserts that the class rejects no properties given at all.
8582
*
8683
* @throws Exception
8784
*/
88-
@Test(expected = IllegalArgumentException.class)
89-
public void preventsNoProperties() throws Exception {
90-
91-
Sort.by(Direction.ASC);
85+
@Test
86+
public void preventsNoProperties() {
87+
assertThatIllegalArgumentException().isThrownBy(() -> Sort.by(Direction.ASC));
9288
}
9389

9490
@Test

src/test/java/org/springframework/data/geo/CircleUnitTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
*/
2929
public class CircleUnitTests {
3030

31-
@Test(expected = IllegalArgumentException.class) // DATACMNS-437
31+
@Test // DATACMNS-437
3232
public void rejectsNullOrigin() {
33-
new Circle(null, new Distance(0));
33+
assertThatIllegalArgumentException().isThrownBy(() -> new Circle(null, new Distance(0)));
3434
}
3535

36-
@Test(expected = IllegalArgumentException.class) // DATACMNS-437
36+
@Test // DATACMNS-437
3737
public void rejectsNegativeRadius() {
38-
new Circle(1, 1, -1);
38+
assertThatIllegalArgumentException().isThrownBy(() -> new Circle(1, 1, -1));
3939
}
4040

4141
@Test // DATACMNS-437

src/test/java/org/springframework/data/geo/GeoResultUnitTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ public void considersSameValuesAsEqual() {
5050
assertThat(fourth.equals(first)).isFalse();
5151
}
5252

53+
@Test
5354
@SuppressWarnings({ "rawtypes", "unchecked" })
54-
@Test(expected = IllegalArgumentException.class) // DATACMNS-437
55+
// DATACMNS-437
5556
public void rejectsNullContent() {
56-
new GeoResult(null, new Distance(2.5));
57+
assertThatIllegalArgumentException().isThrownBy(() -> new GeoResult(null, new Distance(2.5)));
5758
}
5859

5960
@Test // DATACMNS-482

0 commit comments

Comments
 (0)