import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration;
import org.springframework.messaging.Message;
-import org.springframework.messaging.support.GenericMessage;
+import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.annotation.DirtiesContext;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.junit4.SpringRunner;
-@RunWith(SpringJUnit4ClassRunner.class)
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+@RunWith(SpringRunner.class)
@SpringBootTest(classes = { Receiver.class, TestSupportBinderAutoConfiguration.class })
@DirtiesContext
public class ReceiverIntegrationTest {
DummyService dummyService;
@Test
- public void callSomeDummy() {
+ public void callSomeDummy() throws JsonProcessingException {
+ ObjectMapper objectMapper = new ObjectMapper();
String productName = "product";
String productDescription = "productDescription";
Product product = new Product(productName, productDescription);
ArgumentCaptor<String> dummyArgCaptor = ArgumentCaptor.forClass(String.class);
doNothing().when(dummyService).iAmVeryDummy(dummyArgCaptor.capture());
- Message<Product> message = new GenericMessage<>(product);
- source.input().send(message);
+
+ Message<String> message = MessageBuilder
+ .withPayload(objectMapper.writeValueAsString(product))
+ .build();
+ source
+ .input()
+ .send(message);
assertThat(dummyArgCaptor.getValue(), is(product.getName()));
}
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+
import javax.inject.Inject;
import org.junit.Test;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { Sender.class, TestSupportBinderAutoConfiguration.class })
@DirtiesContext
private MessageCollector messageCollector;
@Test
- public void sendSomeProduct() {
- Product product = new Product("hello", "this is some description");
+ public void sendSomeProduct() throws JsonParseException, JsonMappingException, IOException {
+ ObjectMapper objectMapper = new ObjectMapper();
+ Product expected = new Product("hello", "this is some description");
sender.sendMessage("hello");
- Message<Product> received = (Message<Product>) messageCollector.forChannel(source.output()).poll();
+ Message<String> received = (Message<String>) messageCollector.forChannel(source.output()).poll();
+ Product receivedProduct = objectMapper.readValue(received.getPayload().toString(), Product.class);
- assertThat(received.getPayload().getDescription(), is(product.getDescription()));
+ assertThat(receivedProduct.getDescription(), is(expected.getDescription()));
+ assertThat(receivedProduct.getName(), is(expected.getName()));
+
}
}