1 package de.example.spring.kafka;
3 import static org.hamcrest.CoreMatchers.is;
4 import static org.junit.Assert.assertThat;
6 import java.io.IOException;
7 import java.io.StringWriter;
10 import javax.inject.Inject;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.springframework.boot.test.context.SpringBootTest;
15 import org.springframework.cloud.stream.messaging.Source;
16 import org.springframework.cloud.stream.test.binder.MessageCollector;
17 import org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration;
18 import org.springframework.messaging.Message;
19 import org.springframework.test.annotation.DirtiesContext;
20 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
22 import com.fasterxml.jackson.core.JsonParseException;
23 import com.fasterxml.jackson.databind.JsonMappingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
26 @RunWith(SpringJUnit4ClassRunner.class)
27 @SpringBootTest(classes = { Sender.class, TestSupportBinderAutoConfiguration.class })
29 public class SenderIntegrationTest {
38 private MessageCollector messageCollector;
41 public void sendSomeProduct() throws JsonParseException, JsonMappingException, IOException {
42 ObjectMapper objectMapper = new ObjectMapper();
43 Product expected = new Product("hello", "this is some description");
45 sender.sendMessage("hello");
47 Message<String> received = (Message<String>) messageCollector.forChannel(source.output()).poll();
48 Product receivedProduct = objectMapper.readValue(received.getPayload().toString(), Product.class);
50 assertThat(receivedProduct.getDescription(), is(expected.getDescription()));
51 assertThat(receivedProduct.getName(), is(expected.getName()));