Making work integration tests
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 31 Jan 2017 21:04:21 +0000 (22:04 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 31 Jan 2017 21:04:21 +0000 (22:04 +0100)
SpringJava/Kafka/SpringCloudStream/spring-stream-kafka-consumer/src/test/java/de/example/spring/kafka/ReceiverIntegrationTest.java
SpringJava/Kafka/SpringCloudStream/spring-stream-kafka-producer/src/test/java/de/example/spring/kafka/SenderIntegrationTest.java

index c89e565..4867f5d 100644 (file)
@@ -14,11 +14,14 @@ import org.springframework.boot.test.mock.mockito.MockBean;
 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 {
@@ -30,15 +33,21 @@ 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()));
        }
index 2b170b7..b864a28 100644 (file)
@@ -3,6 +3,10 @@ package de.example.spring.kafka;
 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;
@@ -15,6 +19,10 @@ import org.springframework.messaging.Message;
 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
@@ -30,14 +38,18 @@ public class SenderIntegrationTest {
        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()));
+
        }
 
 }