44
44
import org .springframework .context .testfixture .cache .beans .DefaultCacheableService ;
45
45
46
46
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 ;
47
49
48
50
/**
49
51
* Integration tests for {@code @EnableCaching} and its related
@@ -76,7 +78,7 @@ void cacheErrorHandler() {
76
78
void singleCacheManagerBean () {
77
79
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
78
80
ctx .register (SingleCacheManagerConfig .class );
79
- ctx . refresh ();
81
+ assertThatCode ( ctx :: refresh ). doesNotThrowAnyException ();
80
82
ctx .close ();
81
83
}
82
84
@@ -85,20 +87,17 @@ void multipleCacheManagerBeans() {
85
87
@ SuppressWarnings ("resource" )
86
88
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
87
89
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 );
95
94
}
96
95
97
96
@ Test
98
97
void multipleCacheManagerBeans_implementsCachingConfigurer () {
99
98
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
100
99
ctx .register (MultiCacheManagerConfigurer .class );
101
- ctx . refresh (); // does not throw an exception
100
+ assertThatCode ( ctx :: refresh ). doesNotThrowAnyException ();
102
101
ctx .close ();
103
102
}
104
103
@@ -107,35 +106,27 @@ void multipleCachingConfigurers() {
107
106
@ SuppressWarnings ("resource" )
108
107
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
109
108
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" );
116
111
}
117
112
118
113
@ Test
119
114
void noCacheManagerBeans () {
120
115
@ SuppressWarnings ("resource" )
121
116
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
122
117
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 );
130
122
}
131
123
132
124
@ Test
133
125
void emptyConfigSupport () {
134
126
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext (EmptyConfigSupportConfig .class );
135
127
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 )));
139
130
context .close ();
140
131
}
141
132
0 commit comments