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:
Phillip Webb
2019-05-20 10:34:51 -07:00
parent fb26fc3f94
commit 02850f357f
561 changed files with 6592 additions and 10389 deletions

View File

@@ -27,6 +27,7 @@ import org.junit.Test;
import org.springframework.util.SerializationTestUtils;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
@@ -127,13 +128,14 @@ public class MessageHeadersTests {
assertEquals(value, headers.get("test", Integer.class));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testHeaderValueAccessWithIncorrectType() {
Integer value = new Integer(123);
Map<String, Object> map = new HashMap<>();
map.put("test", value);
MessageHeaders headers = new MessageHeaders(map);
assertEquals(value, headers.get("test", String.class));
assertThatIllegalArgumentException().isThrownBy(() ->
headers.get("test", String.class));
}
@Test

View File

@@ -27,6 +27,8 @@ import org.springframework.messaging.MessageHeaders;
import org.springframework.util.InvalidMimeTypeException;
import org.springframework.util.MimeTypeUtils;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@@ -63,20 +65,22 @@ public class DefaultContentTypeResolverTests {
assertEquals(MimeTypeUtils.APPLICATION_JSON, this.resolver.resolve(headers));
}
@Test(expected = InvalidMimeTypeException.class)
@Test
public void resolveInvalidStringContentType() {
Map<String, Object> map = new HashMap<>();
map.put(MessageHeaders.CONTENT_TYPE, "invalidContentType");
MessageHeaders headers = new MessageHeaders(map);
this.resolver.resolve(headers);
assertThatExceptionOfType(InvalidMimeTypeException.class).isThrownBy(() ->
this.resolver.resolve(headers));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void resolveUnknownHeaderType() {
Map<String, Object> map = new HashMap<>();
map.put(MessageHeaders.CONTENT_TYPE, new Integer(1));
MessageHeaders headers = new MessageHeaders(map);
this.resolver.resolve(headers);
assertThatIllegalArgumentException().isThrownBy(() ->
this.resolver.resolve(headers));
}
@Test

View File

@@ -33,6 +33,7 @@ import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.MimeType;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
@@ -127,12 +128,13 @@ public class MappingJackson2MessageConverterTests {
assertSame(myBean, converter.fromMessage(message, MyBean.class));
}
@Test(expected = MessageConversionException.class)
@Test
public void fromMessageInvalidJson() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
String payload = "FooBar";
Message<?> message = MessageBuilder.withPayload(payload.getBytes(StandardCharsets.UTF_8)).build();
converter.fromMessage(message, MyBean.class);
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
converter.fromMessage(message, MyBean.class));
}
@Test

View File

@@ -28,6 +28,7 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -65,18 +66,20 @@ public class MarshallingMessageConverterTests {
assertEquals("Foo", actual.getName());
}
@Test(expected = MessageConversionException.class)
@Test
public void fromMessageInvalidXml() throws Exception {
String payload = "<myBean><name>Foo</name><myBean>";
Message<?> message = MessageBuilder.withPayload(payload.getBytes(StandardCharsets.UTF_8)).build();
this.converter.fromMessage(message, MyBean.class);
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
this.converter.fromMessage(message, MyBean.class));
}
@Test(expected = MessageConversionException.class)
@Test
public void fromMessageValidXmlWithUnknownProperty() throws IOException {
String payload = "<myBean><age>42</age><myBean>";
Message<?> message = MessageBuilder.withPayload(payload.getBytes(StandardCharsets.UTF_8)).build();
this.converter.fromMessage(message, MyBean.class);
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
this.converter.fromMessage(message, MyBean.class));
}
@Test

View File

@@ -33,6 +33,7 @@ import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
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.assertNotNull;
@@ -104,10 +105,11 @@ public class MessageConverterTests {
}
@Test(expected = IllegalArgumentException.class)
@Test
public void setStrictContentTypeMatchWithNoSupportedMimeTypes() {
this.converter = new TestMessageConverter(Collections.<MimeType>emptyList());
this.converter.setStrictContentTypeMatch(true);
assertThatIllegalArgumentException().isThrownBy(() ->
this.converter.setStrictContentTypeMatch(true));
}
@Test

View File

@@ -18,6 +18,7 @@ package org.springframework.messaging.core;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -50,15 +51,17 @@ public class CachingDestinationResolverTests {
verify(resolver, times(1)).resolveDestination("1234");
}
@Test(expected = IllegalArgumentException.class)
@Test
public void noTargetSet() {
CachingDestinationResolverProxy<String> resolverProxy = new CachingDestinationResolverProxy<>();
resolverProxy.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(
resolverProxy::afterPropertiesSet);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void nullTargetThroughConstructor() {
new CachingDestinationResolverProxy<String>(null);
assertThatIllegalArgumentException().isThrownBy(() ->
new CachingDestinationResolverProxy<String>(null));
}
}

View File

@@ -28,6 +28,7 @@ import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.ExecutorSubscribableChannel;
import org.springframework.messaging.support.GenericMessage;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
@@ -74,10 +75,11 @@ public class DestinationResolvingMessagingTemplateTests {
assertSame(message, this.template.message);
}
@Test(expected = IllegalStateException.class)
@Test
public void sendNoDestinationResolver() {
TestDestinationResolvingMessagingTemplate template = new TestDestinationResolvingMessagingTemplate();
template.send("myChannel", new GenericMessage<Object>("payload"));
assertThatIllegalStateException().isThrownBy(() ->
template.send("myChannel", new GenericMessage<Object>("payload")));
}
@Test

View File

@@ -28,6 +28,7 @@ import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.messaging.support.GenericMessage;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
@@ -59,9 +60,10 @@ public class MessageReceivingTemplateTests {
assertSame(expected, actual);
}
@Test(expected = IllegalStateException.class)
@Test
public void receiveMissingDefaultDestination() {
this.template.receive();
assertThatIllegalStateException().isThrownBy(
this.template::receive);
}
@Test

View File

@@ -25,6 +25,7 @@ import org.junit.Test;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
@@ -65,9 +66,10 @@ public class MessageRequestReplyTemplateTests {
assertSame(responseMessage, actual);
}
@Test(expected = IllegalStateException.class)
@Test
public void sendAndReceiveMissingDestination() {
this.template.sendAndReceive(new GenericMessage<Object>("request"));
assertThatIllegalStateException().isThrownBy(() ->
this.template.sendAndReceive(new GenericMessage<Object>("request")));
}
@Test

View File

@@ -36,6 +36,8 @@ import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
@@ -81,10 +83,11 @@ public class MessageSendingTemplateTests {
assertSame(message, this.template.message);
}
@Test(expected = IllegalStateException.class)
@Test
public void sendMissingDestination() {
Message<?> message = new GenericMessage<Object>("payload");
this.template.send(message);
assertThatIllegalStateException().isThrownBy(() ->
this.template.send(message));
}
@Test
@@ -174,7 +177,7 @@ public class MessageSendingTemplateTests {
assertSame(this.template.message, this.postProcessor.getMessage());
}
@Test(expected = MessageConversionException.class)
@Test
public void convertAndSendNoMatchingConverter() {
MessageConverter converter = new CompositeMessageConverter(
@@ -182,7 +185,8 @@ public class MessageSendingTemplateTests {
this.template.setMessageConverter(converter);
this.headers.put(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_XML);
this.template.convertAndSend("home", "payload", new MessageHeaders(this.headers));
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
this.template.convertAndSend("home", "payload", new MessageHeaders(this.headers)));
}

View File

@@ -30,6 +30,7 @@ import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.invocation.ResolvableMethod;
import org.springframework.messaging.support.MessageBuilder;
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.assertTrue;
@@ -71,10 +72,11 @@ public class DestinationVariableMethodArgumentResolverTests {
assertEquals("value", result);
}
@Test(expected = MessageHandlingException.class)
@Test
public void resolveArgumentNotFound() {
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
resolveArgument(this.resolvable.annot(destinationVar().noValue()).arg(), message);
assertThatExceptionOfType(MessageHandlingException.class).isThrownBy(() ->
resolveArgument(this.resolvable.annot(destinationVar().noValue()).arg(), message));
}
@SuppressWarnings({"unchecked", "ConstantConditions"})

View File

@@ -34,6 +34,7 @@ import org.springframework.messaging.handler.invocation.ResolvableMethod;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.support.NativeMessageHeaderAccessor;
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.assertTrue;
@@ -94,10 +95,11 @@ public class HeaderMethodArgumentResolverTests {
this.resolvable.annot(header("nativeHeaders.param1")).arg(), message));
}
@Test(expected = MessageHandlingException.class)
@Test
public void resolveArgumentNotFound() {
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
resolveArgument(this.resolvable.annot(headerPlain()).arg(), message);
assertThatExceptionOfType(MessageHandlingException.class).isThrownBy(() ->
resolveArgument(this.resolvable.annot(headerPlain()).arg(), message));
}
@Test

View File

@@ -31,6 +31,7 @@ import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.messaging.support.NativeMessageHeaderAccessor;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -70,9 +71,10 @@ public class HeadersMethodArgumentResolverTests {
assertEquals("bar", headers.get("foo"));
}
@Test(expected = IllegalStateException.class)
@Test
public void resolveArgumentAnnotatedNotMap() {
resolveArgument(this.resolvable.annotPresent(Headers.class).arg(String.class));
assertThatIllegalStateException().isThrownBy(() ->
resolveArgument(this.resolvable.annotPresent(Headers.class).arg(String.class)));
}
@Test

View File

@@ -27,6 +27,7 @@ import org.springframework.messaging.handler.annotation.MessageExceptionHandler;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@@ -93,14 +94,16 @@ public class AnnotationExceptionHandlerMethodResolverTests {
assertEquals("handleIOException", this.resolver.resolveMethod(exception).getName());
}
@Test(expected = IllegalStateException.class)
@Test
public void ambiguousExceptionMapping() {
new AnnotationExceptionHandlerMethodResolver(AmbiguousController.class);
assertThatIllegalStateException().isThrownBy(() ->
new AnnotationExceptionHandlerMethodResolver(AmbiguousController.class));
}
@Test(expected = IllegalStateException.class)
@Test
public void noExceptionMapping() {
new AnnotationExceptionHandlerMethodResolver(NoExceptionController.class);
assertThatIllegalStateException().isThrownBy(() ->
new AnnotationExceptionHandlerMethodResolver(NoExceptionController.class));
}

View File

@@ -29,6 +29,7 @@ import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.invocation.ResolvableMethod;
import org.springframework.messaging.support.MessageBuilder;
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.assertTrue;
@@ -73,10 +74,11 @@ public class DestinationVariableMethodArgumentResolverTests {
assertEquals("value", result);
}
@Test(expected = MessageHandlingException.class)
@Test
public void resolveArgumentNotFound() throws Exception {
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
this.resolver.resolveArgument(this.resolvable.annot(destinationVar().noValue()).arg(), message);
assertThatExceptionOfType(MessageHandlingException.class).isThrownBy(() ->
this.resolver.resolveArgument(this.resolvable.annot(destinationVar().noValue()).arg(), message));
}
@SuppressWarnings("unused")

View File

@@ -33,6 +33,7 @@ import org.springframework.messaging.handler.invocation.ResolvableMethod;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.support.NativeMessageHeaderAccessor;
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.assertTrue;
@@ -96,10 +97,11 @@ public class HeaderMethodArgumentResolverTests {
this.resolvable.annot(header("nativeHeaders.param1")).arg(), message));
}
@Test(expected = MessageHandlingException.class)
@Test
public void resolveArgumentNotFound() throws Exception {
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
this.resolver.resolveArgument(this.resolvable.annot(headerPlain()).arg(), message);
assertThatExceptionOfType(MessageHandlingException.class).isThrownBy(() ->
this.resolver.resolveArgument(this.resolvable.annot(headerPlain()).arg(), message));
}
@Test

View File

@@ -30,6 +30,7 @@ import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.messaging.support.NativeMessageHeaderAccessor;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -74,9 +75,10 @@ public class HeadersMethodArgumentResolverTests {
assertEquals("bar", headers.get("foo"));
}
@Test(expected = IllegalStateException.class)
@Test
public void resolveArgumentAnnotatedNotMap() throws Exception {
this.resolver.resolveArgument(this.resolvable.annotPresent(Headers.class).arg(String.class), this.message);
assertThatIllegalStateException().isThrownBy(() ->
this.resolver.resolveArgument(this.resolvable.annotPresent(Headers.class).arg(String.class), this.message));
}
@Test

View File

@@ -24,13 +24,11 @@ import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
/**
@@ -73,15 +71,10 @@ public class InvocableHandlerMethodTests {
@Test
public void cannotResolveArg() throws Exception {
try {
Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0, "")).method();
invoke(new Handler(), method);
fail("Expected exception");
}
catch (MethodArgumentResolutionException ex) {
assertNotNull(ex.getMessage());
assertTrue(ex.getMessage().contains("Could not resolve parameter [0]"));
}
Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0, "")).method();
assertThatExceptionOfType(MethodArgumentResolutionException.class).isThrownBy(() ->
invoke(new Handler(), method))
.withMessageContaining("Could not resolve parameter [0]");
}
@Test
@@ -107,88 +100,57 @@ public class InvocableHandlerMethodTests {
@Test
public void exceptionInResolvingArg() throws Exception {
this.resolvers.addResolver(new ExceptionRaisingArgumentResolver());
try {
Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0, "")).method();
invoke(new Handler(), method);
fail("Expected exception");
}
catch (IllegalArgumentException ex) {
// expected - allow HandlerMethodArgumentResolver exceptions to propagate
}
Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0, "")).method();
assertThatIllegalArgumentException().isThrownBy(() ->
invoke(new Handler(), method));
// expected - allow HandlerMethodArgumentResolver exceptions to propagate
}
@Test
public void illegalArgumentException() throws Exception {
this.resolvers.addResolver(new StubArgumentResolver(Integer.class, "__not_an_int__"));
this.resolvers.addResolver(new StubArgumentResolver("value"));
try {
Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0, "")).method();
invoke(new Handler(), method);
fail("Expected exception");
}
catch (IllegalStateException ex) {
assertNotNull("Exception not wrapped", ex.getCause());
assertTrue(ex.getCause() instanceof IllegalArgumentException);
assertTrue(ex.getMessage().contains("Endpoint ["));
assertTrue(ex.getMessage().contains("Method ["));
assertTrue(ex.getMessage().contains("with argument values:"));
assertTrue(ex.getMessage().contains("[0] [type=java.lang.String] [value=__not_an_int__]"));
assertTrue(ex.getMessage().contains("[1] [type=java.lang.String] [value=value"));
}
Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0, "")).method();
assertThatIllegalStateException().isThrownBy(() ->
invoke(new Handler(), method))
.withCauseInstanceOf(IllegalArgumentException.class)
.withMessageContaining("Endpoint [")
.withMessageContaining("Method [")
.withMessageContaining("with argument values:")
.withMessageContaining("[0] [type=java.lang.String] [value=__not_an_int__]")
.withMessageContaining("[1] [type=java.lang.String] [value=value");
}
@Test
public void invocationTargetException() throws Exception {
Handler handler = new Handler();
Method method = ResolvableMethod.on(Handler.class).argTypes(Throwable.class).resolveMethod();
Throwable expected = null;
try {
expected = new RuntimeException("error");
invoke(handler, method, expected);
fail("Expected exception");
}
catch (RuntimeException actual) {
assertSame(expected, actual);
}
try {
expected = new Error("error");
invoke(handler, method, expected);
fail("Expected exception");
}
catch (Error actual) {
assertSame(expected, actual);
}
try {
expected = new Exception("error");
invoke(handler, method, expected);
fail("Expected exception");
}
catch (Exception actual) {
assertSame(expected, actual);
}
try {
expected = new Throwable("error", expected);
invoke(handler, method, expected);
fail("Expected exception");
}
catch (IllegalStateException actual) {
assertNotNull(actual.getCause());
assertSame(expected, actual.getCause());
assertTrue(actual.getMessage().contains("Invocation failure"));
}
RuntimeException runtimeException = new RuntimeException("error");
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
invoke(handler, method, runtimeException))
.isSameAs(runtimeException);
Error error = new Error("error");
assertThatExceptionOfType(Error.class).isThrownBy(() ->
invoke(handler, method, error))
.isSameAs(error);
Exception exception = new Exception("error");
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
invoke(handler, method, exception))
.isSameAs(exception);
Throwable throwable = new Throwable("error", exception);
assertThatIllegalStateException().isThrownBy(() ->
invoke(handler, method, throwable))
.withCause(throwable)
.withMessageContaining("Invocation failure");
}
@Test // Based on SPR-13917 (spring-web)
public void invocationErrorMessage() throws Exception {
this.resolvers.addResolver(new StubArgumentResolver(double.class));
try {
Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0.0)).method();
invoke(new Handler(), method);
fail();
}
catch (IllegalStateException ex) {
assertThat(ex.getMessage(), containsString("Illegal argument"));
}
Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0.0)).method();
assertThatIllegalStateException().isThrownBy(() ->
invoke(new Handler(), method))
.withMessageContaining("Illegal argument");
}
@Nullable

View File

@@ -41,6 +41,7 @@ import org.springframework.util.AntPathMatcher;
import org.springframework.util.Assert;
import org.springframework.util.PathMatcher;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -75,9 +76,10 @@ public class MethodMessageHandlerTests {
this.messageHandler.registerHandler(this.testController);
}
@Test(expected = IllegalStateException.class)
@Test
public void duplicateMapping() {
this.messageHandler.registerHandler(new DuplicateMappingsController());
assertThatIllegalStateException().isThrownBy(() ->
this.messageHandler.registerHandler(new DuplicateMappingsController()));
}
@Test

View File

@@ -32,12 +32,13 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.handler.invocation.MethodArgumentResolutionException;
import org.springframework.messaging.handler.invocation.ResolvableMethod;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
/**
@@ -81,15 +82,10 @@ public class InvocableHandlerMethodTests {
@Test
public void cannotResolveArg() {
try {
Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0, "")).method();
invokeAndBlock(new Handler(), method);
fail("Expected exception");
}
catch (MethodArgumentResolutionException ex) {
assertNotNull(ex.getMessage());
assertTrue(ex.getMessage().contains("Could not resolve parameter [0]"));
}
Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0, "")).method();
assertThatExceptionOfType(MethodArgumentResolutionException.class).isThrownBy(() ->
invokeAndBlock(new Handler(), method))
.withMessageContaining("Could not resolve parameter [0]");
}
@Test
@@ -115,34 +111,24 @@ public class InvocableHandlerMethodTests {
@Test
public void exceptionInResolvingArg() {
this.resolvers.add(new InvocableHandlerMethodTests.ExceptionRaisingArgumentResolver());
try {
Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0, "")).method();
invokeAndBlock(new Handler(), method);
fail("Expected exception");
}
catch (IllegalArgumentException ex) {
// expected - allow HandlerMethodArgumentResolver exceptions to propagate
}
Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0, "")).method();
assertThatIllegalArgumentException().isThrownBy(() ->
invokeAndBlock(new Handler(), method));
}
@Test
public void illegalArgumentException() {
this.resolvers.add(new StubArgumentResolver(Integer.class, "__not_an_int__"));
this.resolvers.add(new StubArgumentResolver("value"));
try {
Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0, "")).method();
invokeAndBlock(new Handler(), method);
fail("Expected exception");
}
catch (IllegalStateException ex) {
assertNotNull("Exception not wrapped", ex.getCause());
assertTrue(ex.getCause() instanceof IllegalArgumentException);
assertTrue(ex.getMessage().contains("Endpoint ["));
assertTrue(ex.getMessage().contains("Method ["));
assertTrue(ex.getMessage().contains("with argument values:"));
assertTrue(ex.getMessage().contains("[0] [type=java.lang.String] [value=__not_an_int__]"));
assertTrue(ex.getMessage().contains("[1] [type=java.lang.String] [value=value"));
}
Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0, "")).method();
assertThatIllegalStateException().isThrownBy(() ->
invokeAndBlock(new Handler(), method))
.withCauseInstanceOf(IllegalArgumentException.class)
.withMessageContaining("Endpoint [")
.withMessageContaining("Method [")
.withMessageContaining("with argument values:")
.withMessageContaining("[0] [type=java.lang.String] [value=__not_an_int__]")
.withMessageContaining("[1] [type=java.lang.String] [value=value");
}
@Test

View File

@@ -47,6 +47,7 @@ import org.springframework.util.PathMatcher;
import org.springframework.util.RouteMatcher;
import org.springframework.util.SimpleRouteMatcher;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
@@ -57,9 +58,10 @@ import static org.junit.Assert.assertEquals;
public class MethodMessageHandlerTests {
@Test(expected = IllegalStateException.class)
@Test
public void duplicateMapping() {
initMethodMessageHandler(DuplicateMappingsController.class);
assertThatIllegalStateException().isThrownBy(() ->
initMethodMessageHandler(DuplicateMappingsController.class));
}
@Test

View File

@@ -44,11 +44,11 @@ import org.springframework.messaging.rsocket.RSocketRequester.ResponseSpec;
import org.springframework.util.MimeTypeUtils;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Unit tests for {@link DefaultRSocketRequester}.
@@ -194,13 +194,9 @@ public class DefaultRSocketRequesterTests {
@Test
public void rejectFluxToMono() {
try {
this.requester.route("").data(Flux.just("a", "b")).retrieveMono(String.class);
fail();
}
catch (IllegalArgumentException ex) {
assertEquals("No RSocket interaction model for Flux request to Mono response.", ex.getMessage());
}
assertThatIllegalArgumentException().isThrownBy(() ->
this.requester.route("").data(Flux.just("a", "b")).retrieveMono(String.class))
.withMessage("No RSocket interaction model for Flux request to Mono response.");
}
private Payload toPayload(String value) {

View File

@@ -38,6 +38,7 @@ import org.springframework.messaging.simp.TestPrincipal;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.scheduling.TaskScheduler;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -176,10 +177,11 @@ public class SimpleBrokerMessageHandlerTests {
assertArrayEquals(new long[] {10000, 10000}, this.messageHandler.getHeartbeatValue());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void startWithHeartbeatValueWithoutTaskScheduler() {
this.messageHandler.setHeartbeatValue(new long[] {10000, 10000});
this.messageHandler.start();
assertThatIllegalArgumentException().isThrownBy(
this.messageHandler::start);
}
@SuppressWarnings("unchecked")

View File

@@ -25,9 +25,9 @@ import org.junit.Test;
import org.springframework.messaging.Message;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
/**
* Unit tests for {@link BufferingStompDecoder}.
@@ -153,21 +153,17 @@ public class BufferingStompDecoderTests {
assertEquals(24, stompDecoder.getBufferSize());
assertEquals(129, (int) stompDecoder.getExpectedContentLength());
try {
String chunk2 = "\nPayload2a";
stompDecoder.decode(toByteBuffer(chunk2));
fail("Expected exception");
}
catch (StompConversionException ex) {
// expected
}
String chunk2 = "\nPayload2a";
assertThatExceptionOfType(StompConversionException.class).isThrownBy(() ->
stompDecoder.decode(toByteBuffer(chunk2)));
}
@Test(expected = StompConversionException.class)
@Test
public void bufferSizeLimit() {
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(STOMP_DECODER, 10);
String payload = "SEND\na:alpha\n\nMessage body";
stompDecoder.decode(toByteBuffer(payload));
assertThatExceptionOfType(StompConversionException.class).isThrownBy(() ->
stompDecoder.decode(toByteBuffer(payload)));
}
@Test
@@ -190,18 +186,20 @@ public class BufferingStompDecoderTests {
assertEquals(0, messages.size());
}
@Test(expected = StompConversionException.class)
@Test
public void invalidEscapeSequence() {
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(STOMP_DECODER, 128);
String payload = "SEND\na:alpha\\x\\n\nMessage body\0";
stompDecoder.decode(toByteBuffer(payload));
assertThatExceptionOfType(StompConversionException.class).isThrownBy(() ->
stompDecoder.decode(toByteBuffer(payload)));
}
@Test(expected = StompConversionException.class)
@Test
public void invalidEscapeSequenceWithSingleSlashAtEndOfHeaderValue() {
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(STOMP_DECODER, 128);
String payload = "SEND\na:alpha\\\n\nMessage body\0";
stompDecoder.decode(toByteBuffer(payload));
assertThatExceptionOfType(StompConversionException.class).isThrownBy(() ->
stompDecoder.decode(toByteBuffer(payload)));
}
private ByteBuffer toByteBuffer(String chunk) {

View File

@@ -49,6 +49,7 @@ import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.Assert;
import org.springframework.util.SocketUtils;
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.assertTrue;
@@ -168,7 +169,7 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
this.responseHandler.expectMessages(send);
}
@Test(expected = MessageDeliveryException.class)
@Test
public void messageDeliveryExceptionIfSystemSessionForwardFails() throws Exception {
logger.debug("Starting test messageDeliveryExceptionIfSystemSessionForwardFails()");
@@ -176,7 +177,8 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
this.eventPublisher.expectBrokerAvailabilityEvent(false);
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND);
this.relay.handleMessage(MessageBuilder.createMessage("test".getBytes(), headers.getMessageHeaders()));
assertThatExceptionOfType(MessageDeliveryException.class).isThrownBy(() ->
this.relay.handleMessage(MessageBuilder.createMessage("test".getBytes(), headers.getMessageHeaders())));
}
@Test

View File

@@ -18,11 +18,11 @@ package org.springframework.messaging.simp.stomp;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Unit tests for {@link StompClientSupport}.
@@ -41,13 +41,8 @@ public class StompClientSupportTests {
}
private void trySetDefaultHeartbeat(long[] heartbeat) {
try {
this.stompClient.setDefaultHeartbeat(heartbeat);
fail("Expected exception");
}
catch (IllegalArgumentException ex) {
// Ignore
}
assertThatIllegalArgumentException().isThrownBy(() ->
this.stompClient.setDefaultHeartbeat(heartbeat));
}
@Test

View File

@@ -26,6 +26,7 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.util.InvalidMimeTypeException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@@ -160,9 +161,10 @@ public class StompDecoderTests {
assertEquals("alpha:bravo\r\n\\", headers.getFirstNativeHeader("a:\r\n\\b"));
}
@Test(expected = StompConversionException.class)
@Test
public void decodeFrameBodyNotAllowed() {
decode("CONNECT\naccept-version:1.2\n\nThe body of the message\0");
assertThatExceptionOfType(StompConversionException.class).isThrownBy(() ->
decode("CONNECT\naccept-version:1.2\n\nThe body of the message\0"));
}
@Test
@@ -226,14 +228,16 @@ public class StompDecoderTests {
assertIncompleteDecode("SEND\ncontent-type:text/plain;charset=U");
}
@Test(expected = InvalidMimeTypeException.class)
@Test
public void decodeFrameWithInvalidContentType() {
assertIncompleteDecode("SEND\ncontent-type:text/plain;charset=U\n\nThe body\0");
assertThatExceptionOfType(InvalidMimeTypeException.class).isThrownBy(() ->
assertIncompleteDecode("SEND\ncontent-type:text/plain;charset=U\n\nThe body\0"));
}
@Test(expected = StompConversionException.class)
@Test
public void decodeFrameWithIncorrectTerminator() {
decode("SEND\ncontent-length:23\n\nThe body of the message*");
assertThatExceptionOfType(StompConversionException.class).isThrownBy(() ->
decode("SEND\ncontent-length:23\n\nThe body of the message*"));
}
@Test

View File

@@ -27,6 +27,7 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.util.IdGenerator;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -75,16 +76,18 @@ public class MessageBuilderTests {
assertEquals("2", message2.getHeaders().get("bar"));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testIdHeaderValueReadOnly() {
UUID id = UUID.randomUUID();
MessageBuilder.withPayload("test").setHeader(MessageHeaders.ID, id);
assertThatIllegalArgumentException().isThrownBy(() ->
MessageBuilder.withPayload("test").setHeader(MessageHeaders.ID, id));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testTimestampValueReadOnly() {
Long timestamp = 12345L;
MessageBuilder.withPayload("test").setHeader(MessageHeaders.TIMESTAMP, timestamp).build();
assertThatIllegalArgumentException().isThrownBy(() ->
MessageBuilder.withPayload("test").setHeader(MessageHeaders.TIMESTAMP, timestamp).build());
}
@Test