Migrate away from ExpectedException (#22922)
* Add limited checkstyles to test code Add a limited set of checkstyle rules to the test codebase to improve code consistency. * Fix checksyle violations in test code * Organize imports to fix checkstyle for test code * Migrate to assertThatExceptionOfType Migrate aware from ExpectedException rules to AssertJ exception assertions. Also include a checkstyle rules to ensure that the the ExpectedException is not accidentally used in the future. See gh-22894
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,13 +19,12 @@ package org.springframework.cache.caffeine;
|
||||
import com.github.benmanes.caffeine.cache.CacheLoader;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.github.benmanes.caffeine.cache.CaffeineSpec;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@@ -36,9 +35,6 @@ import static org.mockito.Mockito.*;
|
||||
*/
|
||||
public class CaffeineCacheManagerTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void testDynamicMode() {
|
||||
CacheManager cm = new CaffeineCacheManager();
|
||||
@@ -187,9 +183,9 @@ public class CaffeineCacheManagerTests {
|
||||
assertNotNull(value);
|
||||
assertEquals("pong", value.get());
|
||||
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("I only know ping");
|
||||
assertNull(cache1.get("foo"));
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
assertNull(cache1.get("foo")))
|
||||
.withMessageContaining("I only know ping");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,7 +20,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.rules.TestName;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
@@ -38,9 +37,6 @@ import org.springframework.cache.support.SimpleCacheManager;
|
||||
*/
|
||||
public abstract class AbstractJCacheTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Rule
|
||||
public final TestName name = new TestName();
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,9 +18,7 @@ package org.springframework.cache.jcache.config;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
@@ -46,6 +44,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -53,9 +52,6 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
public class JCacheJavaConfigTests extends AbstractJCacheAnnotationTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Override
|
||||
protected ApplicationContext getApplicationContext() {
|
||||
return new AnnotationConfigApplicationContext(EnableCachingConfig.class);
|
||||
@@ -116,8 +112,8 @@ public class JCacheJavaConfigTests extends AbstractJCacheAnnotationTests {
|
||||
service.cache("id");
|
||||
|
||||
// This call requires the cache manager to be set
|
||||
thrown.expect(IllegalStateException.class);
|
||||
service.cacheWithException("test", false);
|
||||
assertThatIllegalStateException().isThrownBy(() ->
|
||||
service.cacheWithException("test", false));
|
||||
}
|
||||
finally {
|
||||
context.close();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -37,6 +37,7 @@ import org.springframework.cache.jcache.support.TestableCacheResolverFactory;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
@@ -99,8 +100,8 @@ public class AnnotationCacheOperationSourceTests extends AbstractJCacheTests {
|
||||
|
||||
@Test
|
||||
public void multiAnnotations() {
|
||||
thrown.expect(IllegalStateException.class);
|
||||
getCacheOperation(InvalidCases.class, name.getMethodName());
|
||||
assertThatIllegalStateException().isThrownBy(() ->
|
||||
getCacheOperation(InvalidCases.class, name.getMethodName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,6 +23,8 @@ import javax.cache.annotation.CachePut;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -56,8 +58,8 @@ public class CachePutOperationTests extends AbstractCacheOperationTests<CachePut
|
||||
CacheMethodDetails<CachePut> methodDetails = create(CachePut.class,
|
||||
SampleObject.class, "noCacheValue", Long.class);
|
||||
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
createDefaultOperation(methodDetails);
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
createDefaultOperation(methodDetails));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -65,16 +67,16 @@ public class CachePutOperationTests extends AbstractCacheOperationTests<CachePut
|
||||
CacheMethodDetails<CachePut> methodDetails = create(CachePut.class,
|
||||
SampleObject.class, "multiCacheValues", Long.class, SampleObject.class, SampleObject.class);
|
||||
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
createDefaultOperation(methodDetails);
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
createDefaultOperation(methodDetails));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeWithWrongParameters() {
|
||||
CachePutOperation operation = createSimpleOperation();
|
||||
|
||||
thrown.expect(IllegalStateException.class);
|
||||
operation.getValueParameter(2L);
|
||||
assertThatIllegalStateException().isThrownBy(() ->
|
||||
operation.getValueParameter(2L));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,13 +24,12 @@ import javax.cache.annotation.CacheMethodDetails;
|
||||
import javax.cache.annotation.CacheResolver;
|
||||
import javax.cache.annotation.CacheResult;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.jcache.AbstractJCacheTests;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
@@ -39,10 +38,6 @@ import static org.mockito.BDDMockito.*;
|
||||
*/
|
||||
public class CacheResolverAdapterTests extends AbstractJCacheTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
|
||||
@Test
|
||||
public void resolveSimpleCache() throws Exception {
|
||||
DefaultCacheInvocationContext<?> dummyContext = createDummyContext();
|
||||
@@ -58,8 +53,8 @@ public class CacheResolverAdapterTests extends AbstractJCacheTests {
|
||||
DefaultCacheInvocationContext<?> dummyContext = createDummyContext();
|
||||
CacheResolverAdapter adapter = new CacheResolverAdapter(getCacheResolver(dummyContext, null));
|
||||
|
||||
thrown.expect(IllegalStateException.class);
|
||||
adapter.resolveCaches(dummyContext);
|
||||
assertThatIllegalStateException().isThrownBy(() ->
|
||||
adapter.resolveCaches(dummyContext));
|
||||
}
|
||||
|
||||
protected CacheResolver getCacheResolver(CacheInvocationContext<? extends Annotation> context, String cacheName) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -28,6 +28,7 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -81,8 +82,9 @@ public class CacheResultOperationTests extends AbstractCacheOperationTests<Cache
|
||||
SampleObject.class, "anotherSimpleGet", String.class, Long.class);
|
||||
CacheResultOperation operation = createDefaultOperation(methodDetails);
|
||||
|
||||
thrown.expect(IllegalStateException.class);
|
||||
operation.getAllParameters("bar"); // missing one argument
|
||||
// missing one argument
|
||||
assertThatIllegalStateException().isThrownBy(() ->
|
||||
operation.getAllParameters("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -91,8 +93,9 @@ public class CacheResultOperationTests extends AbstractCacheOperationTests<Cache
|
||||
SampleObject.class, "anotherSimpleGet", String.class, Long.class);
|
||||
CacheResultOperation operation = createDefaultOperation(methodDetails);
|
||||
|
||||
thrown.expect(IllegalStateException.class);
|
||||
operation.getKeyParameters("bar"); // missing one argument
|
||||
// missing one argument
|
||||
assertThatIllegalStateException().isThrownBy(() ->
|
||||
operation.getKeyParameters("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -26,9 +26,7 @@ import javax.cache.annotation.CacheResult;
|
||||
import javax.cache.annotation.CacheValue;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
@@ -57,9 +55,6 @@ public class JCacheErrorHandlerTests {
|
||||
|
||||
private SimpleService simpleService;
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -29,6 +29,7 @@ import org.springframework.cache.interceptor.NamedCacheResolver;
|
||||
import org.springframework.cache.jcache.AbstractJCacheTests;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -47,15 +48,9 @@ public class JCacheInterceptorTests extends AbstractJCacheTests {
|
||||
AnnotatedJCacheableService service = new AnnotatedJCacheableService(cacheManager.getCache("default"));
|
||||
Method m = ReflectionUtils.findMethod(AnnotatedJCacheableService.class, "cache", String.class);
|
||||
|
||||
try {
|
||||
interceptor.execute(dummyInvoker, service, m, new Object[] {"myId"});
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
assertTrue(ex.getMessage().contains("JSR-107 only supports a single cache"));
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
fail("Unexpected: " + ex);
|
||||
}
|
||||
assertThatIllegalStateException().isThrownBy(() ->
|
||||
interceptor.execute(dummyInvoker, service, m, new Object[] {"myId"}))
|
||||
.withMessageContaining("JSR-107 only supports a single cache");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -66,22 +61,15 @@ public class JCacheInterceptorTests extends AbstractJCacheTests {
|
||||
|
||||
AnnotatedJCacheableService service = new AnnotatedJCacheableService(cacheManager.getCache("default"));
|
||||
Method m = ReflectionUtils.findMethod(AnnotatedJCacheableService.class, "cache", String.class);
|
||||
|
||||
try {
|
||||
interceptor.execute(dummyInvoker, service, m, new Object[] {"myId"});
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
assertTrue(ex.getMessage().contains("Cache could not have been resolved for"));
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
fail("Unexpected: " + ex);
|
||||
}
|
||||
assertThatIllegalStateException().isThrownBy(() ->
|
||||
interceptor.execute(dummyInvoker, service, m, new Object[] {"myId"}))
|
||||
.withMessageContaining("Cache could not have been resolved for");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cacheManagerMandatoryIfCacheResolverNotSet() {
|
||||
thrown.expect(IllegalStateException.class);
|
||||
createOperationSource(null, null, null, defaultKeyGenerator);
|
||||
assertThatIllegalStateException().isThrownBy(() ->
|
||||
createOperationSource(null, null, null, defaultKeyGenerator));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.jcache.interceptor;
|
||||
|
||||
import javax.cache.annotation.CacheKey;
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.jcache.support;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,9 +16,7 @@
|
||||
|
||||
package org.springframework.cache.transaction;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCache;
|
||||
@@ -28,6 +26,7 @@ import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -35,15 +34,12 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
public class TransactionAwareCacheDecoratorTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private final PlatformTransactionManager txManager = new CallCountingTransactionManager();
|
||||
|
||||
@Test
|
||||
public void createWithNullTarget() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
new TransactionAwareCacheDecorator(null);
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
new TransactionAwareCacheDecorator(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -35,15 +35,14 @@ import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.mail.MailParseException;
|
||||
import org.springframework.mail.MailSendException;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -53,10 +52,6 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
public class JavaMailSenderTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
|
||||
@Test
|
||||
public void javaMailSenderWithSimpleMessage() throws MessagingException, IOException {
|
||||
MockJavaMailSender sender = new MockJavaMailSender();
|
||||
@@ -511,9 +506,8 @@ public class JavaMailSenderTests {
|
||||
public void testConnectionWithFailure() throws MessagingException {
|
||||
MockJavaMailSender sender = new MockJavaMailSender();
|
||||
sender.setHost(null);
|
||||
|
||||
thrown.expect(MessagingException.class);
|
||||
sender.testConnection();
|
||||
assertThatExceptionOfType(MessagingException.class).isThrownBy(
|
||||
sender::testConnection);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -519,7 +519,8 @@ public class SpringValidatorAdapterTests {
|
||||
.addPropertyNode(f.getName())
|
||||
.addConstraintViolation();
|
||||
}
|
||||
} catch (IllegalAccessException ex) {
|
||||
}
|
||||
catch (IllegalAccessException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user