From fba84c85165a9725a244ed215e19d02b04985bce Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Tue, 31 Jan 2017 22:04:21 +0100 Subject: [PATCH] Making work integration tests --- .../spring/kafka/ReceiverIntegrationTest.java | 21 +++++++++++++++------ .../example/spring/kafka/SenderIntegrationTest.java | 20 ++++++++++++++++---- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/SpringJava/Kafka/SpringCloudStream/spring-stream-kafka-consumer/src/test/java/de/example/spring/kafka/ReceiverIntegrationTest.java b/SpringJava/Kafka/SpringCloudStream/spring-stream-kafka-consumer/src/test/java/de/example/spring/kafka/ReceiverIntegrationTest.java index c89e565..4867f5d 100644 --- a/SpringJava/Kafka/SpringCloudStream/spring-stream-kafka-consumer/src/test/java/de/example/spring/kafka/ReceiverIntegrationTest.java +++ b/SpringJava/Kafka/SpringCloudStream/spring-stream-kafka-consumer/src/test/java/de/example/spring/kafka/ReceiverIntegrationTest.java @@ -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 dummyArgCaptor = ArgumentCaptor.forClass(String.class); doNothing().when(dummyService).iAmVeryDummy(dummyArgCaptor.capture()); - Message message = new GenericMessage<>(product); - source.input().send(message); + + Message message = MessageBuilder + .withPayload(objectMapper.writeValueAsString(product)) + .build(); + source + .input() + .send(message); assertThat(dummyArgCaptor.getValue(), is(product.getName())); } diff --git a/SpringJava/Kafka/SpringCloudStream/spring-stream-kafka-producer/src/test/java/de/example/spring/kafka/SenderIntegrationTest.java b/SpringJava/Kafka/SpringCloudStream/spring-stream-kafka-producer/src/test/java/de/example/spring/kafka/SenderIntegrationTest.java index 2b170b7..b864a28 100644 --- a/SpringJava/Kafka/SpringCloudStream/spring-stream-kafka-producer/src/test/java/de/example/spring/kafka/SenderIntegrationTest.java +++ b/SpringJava/Kafka/SpringCloudStream/spring-stream-kafka-producer/src/test/java/de/example/spring/kafka/SenderIntegrationTest.java @@ -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 received = (Message) messageCollector.forChannel(source.output()).poll(); + Message received = (Message) 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())); + } } -- 2.1.4