Skip to content

Commit 4191958

Browse files
committed
Merge pull request #28559 from neiser
* pr/28559: Polish "Improve AssertJ usage in cache config tests" Improve AssertJ usage in cache config tests Closes gh-28559
2 parents 3ebdaea + baebf71 commit 4191958

File tree

4 files changed

+38
-46
lines changed

4 files changed

+38
-46
lines changed

spring-context/src/test/java/org/springframework/cache/config/CacheAdviceParserTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@
2121
import org.springframework.beans.factory.BeanDefinitionStoreException;
2222
import org.springframework.context.support.GenericXmlApplicationContext;
2323

24-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
24+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2525

2626
/**
2727
* AOP advice specific parsing tests.
2828
*
2929
* @author Stephane Nicoll
3030
*/
31-
public class CacheAdviceParserTests {
31+
class CacheAdviceParserTests {
3232

3333
@Test
34-
public void keyAndKeyGeneratorCannotBeSetTogether() {
35-
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() ->
36-
new GenericXmlApplicationContext("/org/springframework/cache/config/cache-advice-invalid.xml"));
34+
void keyAndKeyGeneratorCannotBeSetTogether() {
35+
assertThatThrownBy(() -> new GenericXmlApplicationContext(
36+
"/org/springframework/cache/config/cache-advice-invalid.xml")
37+
).isInstanceOf(BeanDefinitionStoreException.class);
3738
// TODO better exception handling
3839
}
3940

spring-context/src/test/java/org/springframework/cache/config/CustomInterceptorTests.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
import org.springframework.context.testfixture.cache.beans.DefaultCacheableService;
3838

3939
import static org.assertj.core.api.Assertions.assertThat;
40-
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
40+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4141

4242
/**
4343
* @author Stephane Nicoll
4444
*/
45-
public class CustomInterceptorTests {
45+
class CustomInterceptorTests {
4646

4747
protected ConfigurableApplicationContext ctx;
4848

@@ -60,25 +60,25 @@ public void tearDown() {
6060
}
6161

6262
@Test
63-
public void onlyOneInterceptorIsAvailable() {
63+
void onlyOneInterceptorIsAvailable() {
6464
Map<String, CacheInterceptor> interceptors = this.ctx.getBeansOfType(CacheInterceptor.class);
65-
assertThat(interceptors.size()).as("Only one interceptor should be defined").isEqualTo(1);
65+
assertThat(interceptors).as("Only one interceptor should be defined").hasSize(1);
6666
CacheInterceptor interceptor = interceptors.values().iterator().next();
67-
assertThat(interceptor.getClass()).as("Custom interceptor not defined").isEqualTo(TestCacheInterceptor.class);
67+
assertThat(interceptor).as("Custom interceptor not defined").isInstanceOf(TestCacheInterceptor.class);
6868
}
6969

7070
@Test
71-
public void customInterceptorAppliesWithRuntimeException() {
71+
void customInterceptorAppliesWithRuntimeException() {
7272
Object o = this.cs.throwUnchecked(0L);
7373
// See TestCacheInterceptor
7474
assertThat(o).isEqualTo(55L);
7575
}
7676

7777
@Test
78-
public void customInterceptorAppliesWithCheckedException() {
79-
assertThatRuntimeException()
80-
.isThrownBy(() -> this.cs.throwChecked(0L))
81-
.withCauseExactlyInstanceOf(IOException.class);
78+
void customInterceptorAppliesWithCheckedException() {
79+
assertThatThrownBy(() -> this.cs.throwChecked(0L))
80+
.isInstanceOf(RuntimeException.class)
81+
.hasCauseExactlyInstanceOf(IOException.class);
8282
}
8383

8484

spring-context/src/test/java/org/springframework/cache/config/EnableCachingIntegrationTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
*
4747
* @author Stephane Nicoll
4848
*/
49-
public class EnableCachingIntegrationTests {
49+
class EnableCachingIntegrationTests {
5050

5151
private ConfigurableApplicationContext context;
5252

@@ -60,14 +60,14 @@ public void closeContext() {
6060

6161

6262
@Test
63-
public void fooServiceWithInterface() {
63+
void fooServiceWithInterface() {
6464
this.context = new AnnotationConfigApplicationContext(FooConfig.class);
6565
FooService service = this.context.getBean(FooService.class);
6666
fooGetSimple(service);
6767
}
6868

6969
@Test
70-
public void fooServiceWithInterfaceCglib() {
70+
void fooServiceWithInterfaceCglib() {
7171
this.context = new AnnotationConfigApplicationContext(FooConfigCglib.class);
7272
FooService service = this.context.getBean(FooService.class);
7373
fooGetSimple(service);
@@ -84,7 +84,7 @@ private void fooGetSimple(FooService service) {
8484
}
8585

8686
@Test
87-
public void barServiceWithCacheableInterfaceCglib() {
87+
void barServiceWithCacheableInterfaceCglib() {
8888
this.context = new AnnotationConfigApplicationContext(BarConfigCglib.class);
8989
BarService service = this.context.getBean(BarService.class);
9090
Cache cache = getCache();
@@ -97,7 +97,7 @@ public void barServiceWithCacheableInterfaceCglib() {
9797
}
9898

9999
@Test
100-
public void beanConditionOff() {
100+
void beanConditionOff() {
101101
this.context = new AnnotationConfigApplicationContext(BeanConditionConfig.class);
102102
FooService service = this.context.getBean(FooService.class);
103103
Cache cache = getCache();
@@ -112,7 +112,7 @@ public void beanConditionOff() {
112112
}
113113

114114
@Test
115-
public void beanConditionOn() {
115+
void beanConditionOn() {
116116
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
117117
ctx.setEnvironment(new MockEnvironment().withProperty("bar.enabled", "true"));
118118
ctx.register(BeanConditionConfig.class);

spring-context/src/test/java/org/springframework/cache/config/EnableCachingTests.java

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import org.springframework.context.testfixture.cache.beans.DefaultCacheableService;
4545

4646
import static org.assertj.core.api.Assertions.assertThat;
47+
import static org.assertj.core.api.Assertions.assertThatCode;
48+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4749

4850
/**
4951
* Integration tests for {@code @EnableCaching} and its related
@@ -76,7 +78,7 @@ void cacheErrorHandler() {
7678
void singleCacheManagerBean() {
7779
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
7880
ctx.register(SingleCacheManagerConfig.class);
79-
ctx.refresh();
81+
assertThatCode(ctx::refresh).doesNotThrowAnyException();
8082
ctx.close();
8183
}
8284

@@ -85,20 +87,17 @@ void multipleCacheManagerBeans() {
8587
@SuppressWarnings("resource")
8688
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
8789
ctx.register(MultiCacheManagerConfig.class);
88-
try {
89-
ctx.refresh();
90-
}
91-
catch (IllegalStateException ex) {
92-
assertThat(ex.getMessage().contains("no unique bean of type CacheManager")).isTrue();
93-
assertThat(ex).hasCauseInstanceOf(NoUniqueBeanDefinitionException.class);
94-
}
90+
assertThatThrownBy(ctx::refresh)
91+
.isInstanceOf(IllegalStateException.class)
92+
.hasMessageContaining("no unique bean of type CacheManager")
93+
.hasCauseInstanceOf(NoUniqueBeanDefinitionException.class);
9594
}
9695

9796
@Test
9897
void multipleCacheManagerBeans_implementsCachingConfigurer() {
9998
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
10099
ctx.register(MultiCacheManagerConfigurer.class);
101-
ctx.refresh(); // does not throw an exception
100+
assertThatCode(ctx::refresh).doesNotThrowAnyException();
102101
ctx.close();
103102
}
104103

@@ -107,35 +106,27 @@ void multipleCachingConfigurers() {
107106
@SuppressWarnings("resource")
108107
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
109108
ctx.register(MultiCacheManagerConfigurer.class, EnableCachingConfig.class);
110-
try {
111-
ctx.refresh();
112-
}
113-
catch (IllegalStateException ex) {
114-
assertThat(ex.getMessage().contains("implementations of CachingConfigurer")).isTrue();
115-
}
109+
assertThatThrownBy(ctx::refresh)
110+
.hasMessageContaining("implementations of CachingConfigurer");
116111
}
117112

118113
@Test
119114
void noCacheManagerBeans() {
120115
@SuppressWarnings("resource")
121116
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
122117
ctx.register(EmptyConfig.class);
123-
try {
124-
ctx.refresh();
125-
}
126-
catch (IllegalStateException ex) {
127-
assertThat(ex.getMessage().contains("no bean of type CacheManager")).isTrue();
128-
assertThat(ex).hasCauseInstanceOf(NoSuchBeanDefinitionException.class);
129-
}
118+
assertThatThrownBy(ctx::refresh)
119+
.isInstanceOf(IllegalStateException.class)
120+
.hasMessageContaining("no bean of type CacheManager")
121+
.hasCauseInstanceOf(NoSuchBeanDefinitionException.class);
130122
}
131123

132124
@Test
133125
void emptyConfigSupport() {
134126
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(EmptyConfigSupportConfig.class);
135127
CacheInterceptor ci = context.getBean(CacheInterceptor.class);
136-
assertThat(ci.getCacheResolver()).isNotNull();
137-
assertThat(ci.getCacheResolver().getClass()).isEqualTo(SimpleCacheResolver.class);
138-
assertThat(((SimpleCacheResolver) ci.getCacheResolver()).getCacheManager()).isSameAs(context.getBean(CacheManager.class));
128+
assertThat(ci.getCacheResolver()).isInstanceOfSatisfying(SimpleCacheResolver.class, cacheResolver ->
129+
assertThat(cacheResolver.getCacheManager()).isSameAs(context.getBean(CacheManager.class)));
139130
context.close();
140131
}
141132

0 commit comments

Comments
 (0)