Migrate exception checking tests to use AssertJ
Migrate tests that use `@Test(expectedException=...)` or `try...fail...catch` to use AssertJ's `assertThatException` instead.
This commit is contained in:
@@ -29,11 +29,11 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
@@ -63,11 +63,11 @@ public class EhCacheSupportTests {
|
||||
@Test
|
||||
public void testCacheManagerConflict() {
|
||||
EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
|
||||
cacheManagerFb.setCacheManagerName("myCacheManager");
|
||||
assertEquals(CacheManager.class, cacheManagerFb.getObjectType());
|
||||
assertTrue("Singleton property", cacheManagerFb.isSingleton());
|
||||
cacheManagerFb.afterPropertiesSet();
|
||||
try {
|
||||
cacheManagerFb.setCacheManagerName("myCacheManager");
|
||||
assertEquals(CacheManager.class, cacheManagerFb.getObjectType());
|
||||
assertTrue("Singleton property", cacheManagerFb.isSingleton());
|
||||
cacheManagerFb.afterPropertiesSet();
|
||||
CacheManager cm = cacheManagerFb.getObject();
|
||||
assertTrue("Loaded CacheManager with no caches", cm.getCacheNames().length == 0);
|
||||
Cache myCache1 = cm.getCache("myCache1");
|
||||
@@ -75,11 +75,8 @@ public class EhCacheSupportTests {
|
||||
|
||||
EhCacheManagerFactoryBean cacheManagerFb2 = new EhCacheManagerFactoryBean();
|
||||
cacheManagerFb2.setCacheManagerName("myCacheManager");
|
||||
cacheManagerFb2.afterPropertiesSet();
|
||||
fail("Should have thrown CacheException because of naming conflict");
|
||||
}
|
||||
catch (CacheException ex) {
|
||||
// expected
|
||||
assertThatExceptionOfType(CacheException.class).as("because of naming conflict").isThrownBy(
|
||||
cacheManagerFb2::afterPropertiesSet);
|
||||
}
|
||||
finally {
|
||||
cacheManagerFb.destroy();
|
||||
|
||||
@@ -29,14 +29,16 @@ import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.interceptor.SimpleKeyGenerator;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIOException;
|
||||
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Stephane Nicoll
|
||||
@@ -99,13 +101,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
Object key = createKey(keyItem);
|
||||
assertNull(cache.get(key));
|
||||
|
||||
try {
|
||||
service.cacheWithException(keyItem, true);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (UnsupportedOperationException e) {
|
||||
// This is what we expect
|
||||
}
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
|
||||
service.cacheWithException(keyItem, true));
|
||||
|
||||
Cache.ValueWrapper result = cache.get(key);
|
||||
assertNotNull(result);
|
||||
@@ -120,13 +117,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
Object key = createKey(keyItem);
|
||||
assertNull(cache.get(key));
|
||||
|
||||
try {
|
||||
service.cacheWithException(keyItem, false);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
// This is what we expect
|
||||
}
|
||||
assertThatNullPointerException().isThrownBy(() ->
|
||||
service.cacheWithException(keyItem, false));
|
||||
assertNull(cache.get(key));
|
||||
}
|
||||
|
||||
@@ -137,14 +129,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
|
||||
Object key = createKey(keyItem);
|
||||
assertNull(cache.get(key));
|
||||
|
||||
try {
|
||||
service.cacheWithCheckedException(keyItem, true);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (IOException e) {
|
||||
// This is what we expect
|
||||
}
|
||||
assertThatIOException().isThrownBy(() ->
|
||||
service.cacheWithCheckedException(keyItem, true));
|
||||
|
||||
Cache.ValueWrapper result = cache.get(key);
|
||||
assertNotNull(result);
|
||||
@@ -155,30 +141,26 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
|
||||
@Test
|
||||
public void cacheExceptionRewriteCallStack() {
|
||||
final String keyItem = name.getMethodName();
|
||||
|
||||
UnsupportedOperationException first = null;
|
||||
String keyItem = name.getMethodName();
|
||||
long ref = service.exceptionInvocations();
|
||||
try {
|
||||
service.cacheWithException(keyItem, true);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (UnsupportedOperationException e) {
|
||||
first = e;
|
||||
}
|
||||
// Sanity check, this particular call has called the service
|
||||
assertEquals("First call should not have been cached", ref + 1, service.exceptionInvocations());
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
|
||||
service.cacheWithException(keyItem, true))
|
||||
.satisfies(first -> {
|
||||
// Sanity check, this particular call has called the service
|
||||
// First call should not have been cached
|
||||
assertThat(service.exceptionInvocations()).isEqualTo(ref + 1);
|
||||
|
||||
UnsupportedOperationException second = methodInCallStack(keyItem);
|
||||
// Sanity check, this particular call has *not* called the service
|
||||
assertEquals("Second call should have been cached", ref + 1, service.exceptionInvocations());
|
||||
UnsupportedOperationException second = methodInCallStack(keyItem);
|
||||
// Sanity check, this particular call has *not* called the service
|
||||
// Second call should have been cached
|
||||
assertThat(service.exceptionInvocations()).isEqualTo(ref + 1);
|
||||
|
||||
assertEquals(first.getCause(), second.getCause());
|
||||
assertEquals(first.getMessage(), second.getMessage());
|
||||
assertFalse("Original stack must not contain any reference to methodInCallStack",
|
||||
contain(first, AbstractJCacheAnnotationTests.class.getName(), "methodInCallStack"));
|
||||
assertTrue("Cached stack should have been rewritten with a reference to methodInCallStack",
|
||||
contain(second, AbstractJCacheAnnotationTests.class.getName(), "methodInCallStack"));
|
||||
assertThat(first).hasCause(second.getCause());
|
||||
assertThat(first).hasMessage(second.getMessage());
|
||||
// Original stack must not contain any reference to methodInCallStack
|
||||
assertThat(contain(first, AbstractJCacheAnnotationTests.class.getName(), "methodInCallStack")).isFalse();
|
||||
assertThat(contain(second, AbstractJCacheAnnotationTests.class.getName(), "methodInCallStack")).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -246,13 +228,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
Object value = new Object();
|
||||
assertNull(cache.get(key));
|
||||
|
||||
try {
|
||||
service.putWithException(keyItem, value, true);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (UnsupportedOperationException e) {
|
||||
// This is what we expect
|
||||
}
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
|
||||
service.putWithException(keyItem, value, true));
|
||||
|
||||
Cache.ValueWrapper result = cache.get(key);
|
||||
assertNotNull(result);
|
||||
@@ -268,13 +245,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
Object value = new Object();
|
||||
assertNull(cache.get(key));
|
||||
|
||||
try {
|
||||
service.putWithException(keyItem, value, false);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
// This is what we expect
|
||||
}
|
||||
assertThatNullPointerException().isThrownBy(() ->
|
||||
service.putWithException(keyItem, value, false));
|
||||
assertNull(cache.get(key));
|
||||
}
|
||||
|
||||
@@ -303,13 +275,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
Object value = new Object();
|
||||
assertNull(cache.get(key));
|
||||
|
||||
try {
|
||||
service.earlyPutWithException(keyItem, value, true);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (UnsupportedOperationException e) {
|
||||
// This is what we expect
|
||||
}
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
|
||||
service.earlyPutWithException(keyItem, value, true));
|
||||
|
||||
Cache.ValueWrapper result = cache.get(key);
|
||||
assertNotNull(result);
|
||||
@@ -324,14 +291,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
Object key = createKey(keyItem);
|
||||
Object value = new Object();
|
||||
assertNull(cache.get(key));
|
||||
|
||||
try {
|
||||
service.earlyPutWithException(keyItem, value, false);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
// This is what we expect
|
||||
}
|
||||
assertThatNullPointerException().isThrownBy(() ->
|
||||
service.earlyPutWithException(keyItem, value, false));
|
||||
// This will be cached anyway as the earlyPut has updated the cache before
|
||||
Cache.ValueWrapper result = cache.get(key);
|
||||
assertNotNull(result);
|
||||
@@ -361,13 +322,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
Object value = new Object();
|
||||
cache.put(key, value);
|
||||
|
||||
try {
|
||||
service.removeWithException(keyItem, true);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (UnsupportedOperationException e) {
|
||||
// This is what we expect
|
||||
}
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
|
||||
service.removeWithException(keyItem, true));
|
||||
|
||||
assertNull(cache.get(key));
|
||||
}
|
||||
@@ -381,13 +337,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
Object value = new Object();
|
||||
cache.put(key, value);
|
||||
|
||||
try {
|
||||
service.removeWithException(keyItem, false);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
// This is what we expect
|
||||
}
|
||||
assertThatNullPointerException().isThrownBy(() ->
|
||||
service.removeWithException(keyItem, false));
|
||||
Cache.ValueWrapper wrapper = cache.get(key);
|
||||
assertNotNull(wrapper);
|
||||
assertEquals(value, wrapper.get());
|
||||
@@ -416,13 +367,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
Object value = new Object();
|
||||
cache.put(key, value);
|
||||
|
||||
try {
|
||||
service.earlyRemoveWithException(keyItem, true);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (UnsupportedOperationException e) {
|
||||
// This is what we expect
|
||||
}
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
|
||||
service.earlyRemoveWithException(keyItem, true));
|
||||
assertNull(cache.get(key));
|
||||
}
|
||||
|
||||
@@ -435,13 +381,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
Object value = new Object();
|
||||
cache.put(key, value);
|
||||
|
||||
try {
|
||||
service.earlyRemoveWithException(keyItem, false);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
// This is what we expect
|
||||
}
|
||||
assertThatNullPointerException().isThrownBy(() ->
|
||||
service.earlyRemoveWithException(keyItem, false));
|
||||
// This will be remove anyway as the earlyRemove has removed the cache before
|
||||
assertNull(cache.get(key));
|
||||
}
|
||||
@@ -465,13 +406,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
Object key = createKey(name.getMethodName());
|
||||
cache.put(key, new Object());
|
||||
|
||||
try {
|
||||
service.removeAllWithException(true);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (UnsupportedOperationException e) {
|
||||
// This is what we expect
|
||||
}
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
|
||||
service.removeAllWithException(true));
|
||||
|
||||
assertTrue(isEmpty(cache));
|
||||
}
|
||||
@@ -483,13 +419,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
Object key = createKey(name.getMethodName());
|
||||
cache.put(key, new Object());
|
||||
|
||||
try {
|
||||
service.removeAllWithException(false);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
// This is what we expect
|
||||
}
|
||||
assertThatNullPointerException().isThrownBy(() ->
|
||||
service.removeAllWithException(false));
|
||||
assertNotNull(cache.get(key));
|
||||
}
|
||||
|
||||
@@ -512,13 +443,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
Object key = createKey(name.getMethodName());
|
||||
cache.put(key, new Object());
|
||||
|
||||
try {
|
||||
service.earlyRemoveAllWithException(true);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (UnsupportedOperationException e) {
|
||||
// This is what we expect
|
||||
}
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
|
||||
service.earlyRemoveAllWithException(true));
|
||||
assertTrue(isEmpty(cache));
|
||||
}
|
||||
|
||||
@@ -529,13 +455,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
Object key = createKey(name.getMethodName());
|
||||
cache.put(key, new Object());
|
||||
|
||||
try {
|
||||
service.earlyRemoveAllWithException(false);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
// This is what we expect
|
||||
}
|
||||
assertThatNullPointerException().isThrownBy(() ->
|
||||
service.earlyRemoveAllWithException(false));
|
||||
// This will be remove anyway as the earlyRemove has removed the cache before
|
||||
assertTrue(isEmpty(cache));
|
||||
}
|
||||
|
||||
@@ -38,9 +38,8 @@ 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.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Stephane Nicoll
|
||||
@@ -85,17 +84,9 @@ public class JCacheCustomInterceptorTests {
|
||||
|
||||
@Test
|
||||
public void customInterceptorAppliesWithCheckedException() {
|
||||
try {
|
||||
cs.cacheWithCheckedException("id", true);
|
||||
fail("Should have failed");
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
assertNotNull("missing original exception", e.getCause());
|
||||
assertEquals(IOException.class, e.getCause().getClass());
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail("Wrong exception type " + e);
|
||||
}
|
||||
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
|
||||
cs.cacheWithCheckedException("id", true))
|
||||
.withCauseExactlyInstanceOf(IOException.class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@@ -156,14 +157,16 @@ public class SimpleMailMessageTests {
|
||||
assertTrue(message1.equals(message2));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void testCopyCtorChokesOnNullOriginalMessage() throws Exception {
|
||||
new SimpleMailMessage(null);
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
new SimpleMailMessage(null));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void testCopyToChokesOnNullTargetMessage() throws Exception {
|
||||
new SimpleMailMessage().copyTo(null);
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
new SimpleMailMessage().copyTo(null));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.mail.javamail;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
@@ -69,9 +70,10 @@ public class InternetAddressEditorTests {
|
||||
assertEquals("Whitespace was not stripped", SIMPLE, editor.getAsText());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void simpleBadAddress() {
|
||||
editor.setAsText(BAD);
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
editor.setAsText(BAD));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -42,12 +42,12 @@ import org.springframework.mail.MailSendException;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
@@ -402,18 +402,9 @@ public class JavaMailSenderTests {
|
||||
sender.setUsername("username");
|
||||
sender.setPassword("password");
|
||||
SimpleMailMessage simpleMessage1 = new SimpleMailMessage();
|
||||
try {
|
||||
sender.send(simpleMessage1);
|
||||
fail("Should have thrown MailSendException");
|
||||
}
|
||||
catch (MailSendException ex) {
|
||||
// expected
|
||||
ex.printStackTrace();
|
||||
assertTrue(ex.getFailedMessages() != null);
|
||||
assertEquals(1, ex.getFailedMessages().size());
|
||||
assertSame(simpleMessage1, ex.getFailedMessages().keySet().iterator().next());
|
||||
assertSame(ex.getCause(), ex.getFailedMessages().values().iterator().next());
|
||||
}
|
||||
assertThatExceptionOfType(MailSendException.class).isThrownBy(() ->
|
||||
sender.send(simpleMessage1))
|
||||
.satisfies(ex -> assertThat(ex.getFailedMessages()).containsExactly(entry(simpleMessage1, (Exception) ex.getCause())));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -423,16 +414,9 @@ public class JavaMailSenderTests {
|
||||
sender.setUsername("username");
|
||||
sender.setPassword("password");
|
||||
SimpleMailMessage simpleMessage1 = new SimpleMailMessage();
|
||||
try {
|
||||
sender.send(simpleMessage1);
|
||||
fail("Should have thrown MailSendException");
|
||||
}
|
||||
catch (MailSendException ex) {
|
||||
// expected
|
||||
ex.printStackTrace();
|
||||
assertTrue(ex.getFailedMessages() != null);
|
||||
assertEquals(0, ex.getFailedMessages().size());
|
||||
}
|
||||
assertThatExceptionOfType(MailSendException.class).isThrownBy(() ->
|
||||
sender.send(simpleMessage1))
|
||||
.satisfies(ex -> assertThat(ex.getFailedMessages()).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -41,6 +41,7 @@ import org.springframework.tests.Assume;
|
||||
import org.springframework.tests.TestGroup;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
@@ -131,11 +132,12 @@ public class QuartzSupportTests {
|
||||
bean.destroy();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void jobDetailWithRunnableInsteadOfJob() {
|
||||
JobDetailImpl jobDetail = new JobDetailImpl();
|
||||
jobDetail.setJobClass((Class) DummyRunnable.class);
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
jobDetail.setJobClass((Class) DummyRunnable.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -29,9 +29,9 @@ import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
import org.springframework.validation.beanvalidation.BeanValidationPostProcessor;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
@@ -44,14 +44,9 @@ public class BeanValidationPostProcessorTests {
|
||||
ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
|
||||
ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
|
||||
ac.registerBeanDefinition("bean", new RootBeanDefinition(NotNullConstrainedBean.class));
|
||||
try {
|
||||
ac.refresh();
|
||||
fail("Should have thrown BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
assertTrue(ex.getRootCause().getMessage().contains("testBean"));
|
||||
assertTrue(ex.getRootCause().getMessage().contains("invalid"));
|
||||
}
|
||||
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(
|
||||
ac::refresh)
|
||||
.satisfies(ex -> assertThat(ex.getRootCause().getMessage()).contains("testBean", "invalid"));
|
||||
ac.close();
|
||||
}
|
||||
|
||||
@@ -87,14 +82,9 @@ public class BeanValidationPostProcessorTests {
|
||||
bd.getPropertyValues().add("testBean", new TestBean());
|
||||
bd.getPropertyValues().add("stringValue", "s");
|
||||
ac.registerBeanDefinition("bean", bd);
|
||||
try {
|
||||
ac.refresh();
|
||||
fail("Should have thrown BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
assertTrue(ex.getRootCause().getMessage().contains("stringValue"));
|
||||
assertTrue(ex.getRootCause().getMessage().contains("invalid"));
|
||||
}
|
||||
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(
|
||||
ac::refresh)
|
||||
.satisfies(ex -> assertThat(ex.getRootCause().getMessage()).contains("stringValue", "invalid"));
|
||||
ac.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.validation.beanvalidation2;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import javax.validation.ValidationException;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.NotNull;
|
||||
@@ -41,9 +42,9 @@ import org.springframework.validation.beanvalidation.CustomValidatorBean;
|
||||
import org.springframework.validation.beanvalidation.MethodValidationInterceptor;
|
||||
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
@@ -76,52 +77,20 @@ public class MethodValidationTests {
|
||||
|
||||
private void doTestProxyValidation(MyValidInterface<String> proxy) {
|
||||
assertNotNull(proxy.myValidMethod("value", 5));
|
||||
try {
|
||||
assertNotNull(proxy.myValidMethod("value", 15));
|
||||
fail("Should have thrown ValidationException");
|
||||
}
|
||||
catch (javax.validation.ValidationException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
assertNotNull(proxy.myValidMethod(null, 5));
|
||||
fail("Should have thrown ValidationException");
|
||||
}
|
||||
catch (javax.validation.ValidationException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
assertNotNull(proxy.myValidMethod("value", 0));
|
||||
fail("Should have thrown ValidationException");
|
||||
}
|
||||
catch (javax.validation.ValidationException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
assertThatExceptionOfType(ValidationException.class).isThrownBy(() ->
|
||||
proxy.myValidMethod("value", 15));
|
||||
assertThatExceptionOfType(ValidationException.class).isThrownBy(() ->
|
||||
proxy.myValidMethod(null, 5));
|
||||
assertThatExceptionOfType(ValidationException.class).isThrownBy(() ->
|
||||
proxy.myValidMethod("value", 0));
|
||||
proxy.myValidAsyncMethod("value", 5);
|
||||
try {
|
||||
proxy.myValidAsyncMethod("value", 15);
|
||||
fail("Should have thrown ValidationException");
|
||||
}
|
||||
catch (javax.validation.ValidationException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
proxy.myValidAsyncMethod(null, 5);
|
||||
fail("Should have thrown ValidationException");
|
||||
}
|
||||
catch (javax.validation.ValidationException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
assertThatExceptionOfType(ValidationException.class).isThrownBy(() ->
|
||||
proxy.myValidAsyncMethod("value", 15));
|
||||
assertThatExceptionOfType(ValidationException.class).isThrownBy(() ->
|
||||
proxy.myValidAsyncMethod(null, 5));
|
||||
assertEquals("myValue", proxy.myGenericMethod("myValue"));
|
||||
try {
|
||||
proxy.myGenericMethod(null);
|
||||
fail("Should have thrown ValidationException");
|
||||
}
|
||||
catch (javax.validation.ValidationException ex) {
|
||||
// expected
|
||||
}
|
||||
assertThatExceptionOfType(ValidationException.class).isThrownBy(() ->
|
||||
proxy.myGenericMethod(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -52,13 +52,13 @@ import org.springframework.validation.FieldError;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
@@ -77,12 +77,8 @@ public class ValidatorFactoryTests {
|
||||
assertEquals(2, result.size());
|
||||
for (ConstraintViolation<ValidPerson> cv : result) {
|
||||
String path = cv.getPropertyPath().toString();
|
||||
if ("name".equals(path) || "address.street".equals(path)) {
|
||||
assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull);
|
||||
}
|
||||
else {
|
||||
fail("Invalid constraint violation with path '" + path + "'");
|
||||
}
|
||||
assertThat(path).matches(actual -> "name".equals(actual) || "address.street".equals(actual));
|
||||
assertThat(cv.getConstraintDescriptor().getAnnotation()).isInstanceOf(NotNull.class);
|
||||
}
|
||||
|
||||
Validator nativeValidator = validator.unwrap(Validator.class);
|
||||
@@ -105,12 +101,8 @@ public class ValidatorFactoryTests {
|
||||
assertEquals(2, result.size());
|
||||
for (ConstraintViolation<ValidPerson> cv : result) {
|
||||
String path = cv.getPropertyPath().toString();
|
||||
if ("name".equals(path) || "address.street".equals(path)) {
|
||||
assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull);
|
||||
}
|
||||
else {
|
||||
fail("Invalid constraint violation with path '" + path + "'");
|
||||
}
|
||||
assertThat(path).matches(actual -> "name".equals(actual) || "address.street".equals(actual));
|
||||
assertThat(cv.getConstraintDescriptor().getAnnotation()).isInstanceOf(NotNull.class);
|
||||
}
|
||||
|
||||
Validator nativeValidator = validator.unwrap(Validator.class);
|
||||
|
||||
Reference in New Issue
Block a user