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
This commit is contained in:
commit
4191958e00
|
@ -21,19 +21,20 @@ import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AOP advice specific parsing tests.
|
* AOP advice specific parsing tests.
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
public class CacheAdviceParserTests {
|
class CacheAdviceParserTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void keyAndKeyGeneratorCannotBeSetTogether() {
|
void keyAndKeyGeneratorCannotBeSetTogether() {
|
||||||
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() ->
|
assertThatThrownBy(() -> new GenericXmlApplicationContext(
|
||||||
new GenericXmlApplicationContext("/org/springframework/cache/config/cache-advice-invalid.xml"));
|
"/org/springframework/cache/config/cache-advice-invalid.xml")
|
||||||
|
).isInstanceOf(BeanDefinitionStoreException.class);
|
||||||
// TODO better exception handling
|
// TODO better exception handling
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,12 +37,12 @@ import org.springframework.context.testfixture.cache.beans.CacheableService;
|
||||||
import org.springframework.context.testfixture.cache.beans.DefaultCacheableService;
|
import org.springframework.context.testfixture.cache.beans.DefaultCacheableService;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
public class CustomInterceptorTests {
|
class CustomInterceptorTests {
|
||||||
|
|
||||||
protected ConfigurableApplicationContext ctx;
|
protected ConfigurableApplicationContext ctx;
|
||||||
|
|
||||||
|
@ -60,25 +60,25 @@ public class CustomInterceptorTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onlyOneInterceptorIsAvailable() {
|
void onlyOneInterceptorIsAvailable() {
|
||||||
Map<String, CacheInterceptor> interceptors = this.ctx.getBeansOfType(CacheInterceptor.class);
|
Map<String, CacheInterceptor> interceptors = this.ctx.getBeansOfType(CacheInterceptor.class);
|
||||||
assertThat(interceptors.size()).as("Only one interceptor should be defined").isEqualTo(1);
|
assertThat(interceptors).as("Only one interceptor should be defined").hasSize(1);
|
||||||
CacheInterceptor interceptor = interceptors.values().iterator().next();
|
CacheInterceptor interceptor = interceptors.values().iterator().next();
|
||||||
assertThat(interceptor.getClass()).as("Custom interceptor not defined").isEqualTo(TestCacheInterceptor.class);
|
assertThat(interceptor).as("Custom interceptor not defined").isInstanceOf(TestCacheInterceptor.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void customInterceptorAppliesWithRuntimeException() {
|
void customInterceptorAppliesWithRuntimeException() {
|
||||||
Object o = this.cs.throwUnchecked(0L);
|
Object o = this.cs.throwUnchecked(0L);
|
||||||
// See TestCacheInterceptor
|
// See TestCacheInterceptor
|
||||||
assertThat(o).isEqualTo(55L);
|
assertThat(o).isEqualTo(55L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void customInterceptorAppliesWithCheckedException() {
|
void customInterceptorAppliesWithCheckedException() {
|
||||||
assertThatRuntimeException()
|
assertThatThrownBy(() -> this.cs.throwChecked(0L))
|
||||||
.isThrownBy(() -> this.cs.throwChecked(0L))
|
.isInstanceOf(RuntimeException.class)
|
||||||
.withCauseExactlyInstanceOf(IOException.class);
|
.hasCauseExactlyInstanceOf(IOException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,7 @@ import static org.springframework.context.testfixture.cache.CacheTestUtils.asser
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
public class EnableCachingIntegrationTests {
|
class EnableCachingIntegrationTests {
|
||||||
|
|
||||||
private ConfigurableApplicationContext context;
|
private ConfigurableApplicationContext context;
|
||||||
|
|
||||||
|
@ -60,14 +60,14 @@ public class EnableCachingIntegrationTests {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void fooServiceWithInterface() {
|
void fooServiceWithInterface() {
|
||||||
this.context = new AnnotationConfigApplicationContext(FooConfig.class);
|
this.context = new AnnotationConfigApplicationContext(FooConfig.class);
|
||||||
FooService service = this.context.getBean(FooService.class);
|
FooService service = this.context.getBean(FooService.class);
|
||||||
fooGetSimple(service);
|
fooGetSimple(service);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void fooServiceWithInterfaceCglib() {
|
void fooServiceWithInterfaceCglib() {
|
||||||
this.context = new AnnotationConfigApplicationContext(FooConfigCglib.class);
|
this.context = new AnnotationConfigApplicationContext(FooConfigCglib.class);
|
||||||
FooService service = this.context.getBean(FooService.class);
|
FooService service = this.context.getBean(FooService.class);
|
||||||
fooGetSimple(service);
|
fooGetSimple(service);
|
||||||
|
@ -84,7 +84,7 @@ public class EnableCachingIntegrationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void barServiceWithCacheableInterfaceCglib() {
|
void barServiceWithCacheableInterfaceCglib() {
|
||||||
this.context = new AnnotationConfigApplicationContext(BarConfigCglib.class);
|
this.context = new AnnotationConfigApplicationContext(BarConfigCglib.class);
|
||||||
BarService service = this.context.getBean(BarService.class);
|
BarService service = this.context.getBean(BarService.class);
|
||||||
Cache cache = getCache();
|
Cache cache = getCache();
|
||||||
|
@ -97,7 +97,7 @@ public class EnableCachingIntegrationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void beanConditionOff() {
|
void beanConditionOff() {
|
||||||
this.context = new AnnotationConfigApplicationContext(BeanConditionConfig.class);
|
this.context = new AnnotationConfigApplicationContext(BeanConditionConfig.class);
|
||||||
FooService service = this.context.getBean(FooService.class);
|
FooService service = this.context.getBean(FooService.class);
|
||||||
Cache cache = getCache();
|
Cache cache = getCache();
|
||||||
|
@ -112,7 +112,7 @@ public class EnableCachingIntegrationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void beanConditionOn() {
|
void beanConditionOn() {
|
||||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
ctx.setEnvironment(new MockEnvironment().withProperty("bar.enabled", "true"));
|
ctx.setEnvironment(new MockEnvironment().withProperty("bar.enabled", "true"));
|
||||||
ctx.register(BeanConditionConfig.class);
|
ctx.register(BeanConditionConfig.class);
|
||||||
|
|
|
@ -44,6 +44,8 @@ import org.springframework.context.testfixture.cache.beans.CacheableService;
|
||||||
import org.springframework.context.testfixture.cache.beans.DefaultCacheableService;
|
import org.springframework.context.testfixture.cache.beans.DefaultCacheableService;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Integration tests for {@code @EnableCaching} and its related
|
* Integration tests for {@code @EnableCaching} and its related
|
||||||
|
@ -76,7 +78,7 @@ class EnableCachingTests extends AbstractCacheAnnotationTests {
|
||||||
void singleCacheManagerBean() {
|
void singleCacheManagerBean() {
|
||||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
ctx.register(SingleCacheManagerConfig.class);
|
ctx.register(SingleCacheManagerConfig.class);
|
||||||
ctx.refresh();
|
assertThatCode(ctx::refresh).doesNotThrowAnyException();
|
||||||
ctx.close();
|
ctx.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,20 +87,17 @@ class EnableCachingTests extends AbstractCacheAnnotationTests {
|
||||||
@SuppressWarnings("resource")
|
@SuppressWarnings("resource")
|
||||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
ctx.register(MultiCacheManagerConfig.class);
|
ctx.register(MultiCacheManagerConfig.class);
|
||||||
try {
|
assertThatThrownBy(ctx::refresh)
|
||||||
ctx.refresh();
|
.isInstanceOf(IllegalStateException.class)
|
||||||
}
|
.hasMessageContaining("no unique bean of type CacheManager")
|
||||||
catch (IllegalStateException ex) {
|
.hasCauseInstanceOf(NoUniqueBeanDefinitionException.class);
|
||||||
assertThat(ex.getMessage().contains("no unique bean of type CacheManager")).isTrue();
|
|
||||||
assertThat(ex).hasCauseInstanceOf(NoUniqueBeanDefinitionException.class);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void multipleCacheManagerBeans_implementsCachingConfigurer() {
|
void multipleCacheManagerBeans_implementsCachingConfigurer() {
|
||||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
ctx.register(MultiCacheManagerConfigurer.class);
|
ctx.register(MultiCacheManagerConfigurer.class);
|
||||||
ctx.refresh(); // does not throw an exception
|
assertThatCode(ctx::refresh).doesNotThrowAnyException();
|
||||||
ctx.close();
|
ctx.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,12 +106,8 @@ class EnableCachingTests extends AbstractCacheAnnotationTests {
|
||||||
@SuppressWarnings("resource")
|
@SuppressWarnings("resource")
|
||||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
ctx.register(MultiCacheManagerConfigurer.class, EnableCachingConfig.class);
|
ctx.register(MultiCacheManagerConfigurer.class, EnableCachingConfig.class);
|
||||||
try {
|
assertThatThrownBy(ctx::refresh)
|
||||||
ctx.refresh();
|
.hasMessageContaining("implementations of CachingConfigurer");
|
||||||
}
|
|
||||||
catch (IllegalStateException ex) {
|
|
||||||
assertThat(ex.getMessage().contains("implementations of CachingConfigurer")).isTrue();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -120,22 +115,18 @@ class EnableCachingTests extends AbstractCacheAnnotationTests {
|
||||||
@SuppressWarnings("resource")
|
@SuppressWarnings("resource")
|
||||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
ctx.register(EmptyConfig.class);
|
ctx.register(EmptyConfig.class);
|
||||||
try {
|
assertThatThrownBy(ctx::refresh)
|
||||||
ctx.refresh();
|
.isInstanceOf(IllegalStateException.class)
|
||||||
}
|
.hasMessageContaining("no bean of type CacheManager")
|
||||||
catch (IllegalStateException ex) {
|
.hasCauseInstanceOf(NoSuchBeanDefinitionException.class);
|
||||||
assertThat(ex.getMessage().contains("no bean of type CacheManager")).isTrue();
|
|
||||||
assertThat(ex).hasCauseInstanceOf(NoSuchBeanDefinitionException.class);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void emptyConfigSupport() {
|
void emptyConfigSupport() {
|
||||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(EmptyConfigSupportConfig.class);
|
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(EmptyConfigSupportConfig.class);
|
||||||
CacheInterceptor ci = context.getBean(CacheInterceptor.class);
|
CacheInterceptor ci = context.getBean(CacheInterceptor.class);
|
||||||
assertThat(ci.getCacheResolver()).isNotNull();
|
assertThat(ci.getCacheResolver()).isInstanceOfSatisfying(SimpleCacheResolver.class, cacheResolver ->
|
||||||
assertThat(ci.getCacheResolver().getClass()).isEqualTo(SimpleCacheResolver.class);
|
assertThat(cacheResolver.getCacheManager()).isSameAs(context.getBean(CacheManager.class)));
|
||||||
assertThat(((SimpleCacheResolver) ci.getCacheResolver()).getCacheManager()).isSameAs(context.getBean(CacheManager.class));
|
|
||||||
context.close();
|
context.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue