Throw a proper exception if no convert is found

Prior to this commit, no exception was raised if a message could not
be converted to the requested payload because no suitable converter
were found.

This commit adds an explicit check if the converted payload is null.

Issue: SPR-11817
This commit is contained in:
Stephane Nicoll
2014-05-28 16:48:48 +02:00
parent 8dec1db914
commit 12a9df8a1c
4 changed files with 53 additions and 6 deletions

View File

@@ -17,13 +17,23 @@
package org.springframework.messaging.core;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.messaging.Message;
import org.springframework.messaging.converter.GenericMessageConverter;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.messaging.support.GenericMessage;
import static org.hamcrest.CoreMatchers.isA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import java.io.Writer;
/**
* Unit tests for receiving operations in {@link AbstractMessagingTemplate}.
*
@@ -33,6 +43,9 @@ import static org.junit.Assert.assertSame;
*/
public class MessageReceivingTemplateTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
private TestMessagingTemplate template;
@@ -88,6 +101,29 @@ public class MessageReceivingTemplateTests {
assertSame("payload", payload);
}
@Test
public void receiveAndConvertFailed() {
Message<?> expected = new GenericMessage<Object>("not a number test");
this.template.setReceiveMessage(expected);
this.template.setMessageConverter(new GenericMessageConverter(new DefaultConversionService()));
thrown.expect(MessageConversionException.class);
thrown.expectCause(isA(ConversionFailedException.class));
this.template.receiveAndConvert("somewhere", Integer.class);
}
@Test
public void receiveAndConvertNoConverter() {
Message<?> expected = new GenericMessage<Object>("payload");
this.template.setDefaultDestination("home");
this.template.setReceiveMessage(expected);
this.template.setMessageConverter(new GenericMessageConverter(new DefaultConversionService()));
thrown.expect(MessageConversionException.class);
thrown.expectMessage("payload");
this.template.receiveAndConvert(Writer.class);
}
private static class TestMessagingTemplate extends AbstractMessagingTemplate<String> {