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:
Stephane Nicoll 2022-06-03 10:45:40 +02:00
commit 4191958e00
4 changed files with 38 additions and 46 deletions

View File

@ -21,19 +21,20 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanDefinitionStoreException;
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.
*
* @author Stephane Nicoll
*/
public class CacheAdviceParserTests {
class CacheAdviceParserTests {
@Test
public void keyAndKeyGeneratorCannotBeSetTogether() {
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() ->
new GenericXmlApplicationContext("/org/springframework/cache/config/cache-advice-invalid.xml"));
void keyAndKeyGeneratorCannotBeSetTogether() {
assertThatThrownBy(() -> new GenericXmlApplicationContext(
"/org/springframework/cache/config/cache-advice-invalid.xml")
).isInstanceOf(BeanDefinitionStoreException.class);
// TODO better exception handling
}

View File

@ -37,12 +37,12 @@ import org.springframework.context.testfixture.cache.beans.CacheableService;
import org.springframework.context.testfixture.cache.beans.DefaultCacheableService;
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
*/
public class CustomInterceptorTests {
class CustomInterceptorTests {
protected ConfigurableApplicationContext ctx;
@ -60,25 +60,25 @@ public class CustomInterceptorTests {
}
@Test
public void onlyOneInterceptorIsAvailable() {
void onlyOneInterceptorIsAvailable() {
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();
assertThat(interceptor.getClass()).as("Custom interceptor not defined").isEqualTo(TestCacheInterceptor.class);
assertThat(interceptor).as("Custom interceptor not defined").isInstanceOf(TestCacheInterceptor.class);
}
@Test
public void customInterceptorAppliesWithRuntimeException() {
void customInterceptorAppliesWithRuntimeException() {
Object o = this.cs.throwUnchecked(0L);
// See TestCacheInterceptor
assertThat(o).isEqualTo(55L);
}
@Test
public void customInterceptorAppliesWithCheckedException() {
assertThatRuntimeException()
.isThrownBy(() -> this.cs.throwChecked(0L))
.withCauseExactlyInstanceOf(IOException.class);
void customInterceptorAppliesWithCheckedException() {
assertThatThrownBy(() -> this.cs.throwChecked(0L))
.isInstanceOf(RuntimeException.class)
.hasCauseExactlyInstanceOf(IOException.class);
}

View File

@ -46,7 +46,7 @@ import static org.springframework.context.testfixture.cache.CacheTestUtils.asser
*
* @author Stephane Nicoll
*/
public class EnableCachingIntegrationTests {
class EnableCachingIntegrationTests {
private ConfigurableApplicationContext context;
@ -60,14 +60,14 @@ public class EnableCachingIntegrationTests {
@Test
public void fooServiceWithInterface() {
void fooServiceWithInterface() {
this.context = new AnnotationConfigApplicationContext(FooConfig.class);
FooService service = this.context.getBean(FooService.class);
fooGetSimple(service);
}
@Test
public void fooServiceWithInterfaceCglib() {
void fooServiceWithInterfaceCglib() {
this.context = new AnnotationConfigApplicationContext(FooConfigCglib.class);
FooService service = this.context.getBean(FooService.class);
fooGetSimple(service);
@ -84,7 +84,7 @@ public class EnableCachingIntegrationTests {
}
@Test
public void barServiceWithCacheableInterfaceCglib() {
void barServiceWithCacheableInterfaceCglib() {
this.context = new AnnotationConfigApplicationContext(BarConfigCglib.class);
BarService service = this.context.getBean(BarService.class);
Cache cache = getCache();
@ -97,7 +97,7 @@ public class EnableCachingIntegrationTests {
}
@Test
public void beanConditionOff() {
void beanConditionOff() {
this.context = new AnnotationConfigApplicationContext(BeanConditionConfig.class);
FooService service = this.context.getBean(FooService.class);
Cache cache = getCache();
@ -112,7 +112,7 @@ public class EnableCachingIntegrationTests {
}
@Test
public void beanConditionOn() {
void beanConditionOn() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.setEnvironment(new MockEnvironment().withProperty("bar.enabled", "true"));
ctx.register(BeanConditionConfig.class);

View File

@ -44,6 +44,8 @@ import org.springframework.context.testfixture.cache.beans.CacheableService;
import org.springframework.context.testfixture.cache.beans.DefaultCacheableService;
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
@ -76,7 +78,7 @@ class EnableCachingTests extends AbstractCacheAnnotationTests {
void singleCacheManagerBean() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(SingleCacheManagerConfig.class);
ctx.refresh();
assertThatCode(ctx::refresh).doesNotThrowAnyException();
ctx.close();
}
@ -85,20 +87,17 @@ class EnableCachingTests extends AbstractCacheAnnotationTests {
@SuppressWarnings("resource")
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(MultiCacheManagerConfig.class);
try {
ctx.refresh();
}
catch (IllegalStateException ex) {
assertThat(ex.getMessage().contains("no unique bean of type CacheManager")).isTrue();
assertThat(ex).hasCauseInstanceOf(NoUniqueBeanDefinitionException.class);
}
assertThatThrownBy(ctx::refresh)
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("no unique bean of type CacheManager")
.hasCauseInstanceOf(NoUniqueBeanDefinitionException.class);
}
@Test
void multipleCacheManagerBeans_implementsCachingConfigurer() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(MultiCacheManagerConfigurer.class);
ctx.refresh(); // does not throw an exception
assertThatCode(ctx::refresh).doesNotThrowAnyException();
ctx.close();
}
@ -107,12 +106,8 @@ class EnableCachingTests extends AbstractCacheAnnotationTests {
@SuppressWarnings("resource")
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(MultiCacheManagerConfigurer.class, EnableCachingConfig.class);
try {
ctx.refresh();
}
catch (IllegalStateException ex) {
assertThat(ex.getMessage().contains("implementations of CachingConfigurer")).isTrue();
}
assertThatThrownBy(ctx::refresh)
.hasMessageContaining("implementations of CachingConfigurer");
}
@Test
@ -120,22 +115,18 @@ class EnableCachingTests extends AbstractCacheAnnotationTests {
@SuppressWarnings("resource")
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(EmptyConfig.class);
try {
ctx.refresh();
}
catch (IllegalStateException ex) {
assertThat(ex.getMessage().contains("no bean of type CacheManager")).isTrue();
assertThat(ex).hasCauseInstanceOf(NoSuchBeanDefinitionException.class);
}
assertThatThrownBy(ctx::refresh)
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("no bean of type CacheManager")
.hasCauseInstanceOf(NoSuchBeanDefinitionException.class);
}
@Test
void emptyConfigSupport() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(EmptyConfigSupportConfig.class);
CacheInterceptor ci = context.getBean(CacheInterceptor.class);
assertThat(ci.getCacheResolver()).isNotNull();
assertThat(ci.getCacheResolver().getClass()).isEqualTo(SimpleCacheResolver.class);
assertThat(((SimpleCacheResolver) ci.getCacheResolver()).getCacheManager()).isSameAs(context.getBean(CacheManager.class));
assertThat(ci.getCacheResolver()).isInstanceOfSatisfying(SimpleCacheResolver.class, cacheResolver ->
assertThat(cacheResolver.getCacheManager()).isSameAs(context.getBean(CacheManager.class)));
context.close();
}