b864a28562761bc9884afb8f8dfb1e2b30a9679b
[JavaForFun] /
1 package de.example.spring.kafka;
2
3 import static org.hamcrest.CoreMatchers.is;
4 import static org.junit.Assert.assertThat;
5
6 import java.io.IOException;
7 import java.io.StringWriter;
8 import java.io.Writer;
9
10 import javax.inject.Inject;
11
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;
21
22 import com.fasterxml.jackson.core.JsonParseException;
23 import com.fasterxml.jackson.databind.JsonMappingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25
26 @RunWith(SpringJUnit4ClassRunner.class)
27 @SpringBootTest(classes = { Sender.class, TestSupportBinderAutoConfiguration.class })
28 @DirtiesContext
29 public class SenderIntegrationTest {
30
31         @Inject
32         Source source;
33         
34         @Inject
35         Sender sender;
36         
37         @Inject
38         private MessageCollector messageCollector;
39         
40         @Test
41         public void sendSomeProduct() throws JsonParseException, JsonMappingException, IOException {
42                 ObjectMapper objectMapper = new ObjectMapper();
43                 Product expected = new Product("hello", "this is some description");
44                 
45                 sender.sendMessage("hello");
46                 
47                 Message<String> received = (Message<String>) messageCollector.forChannel(source.output()).poll();
48                 Product receivedProduct = objectMapper.readValue(received.getPayload().toString(), Product.class);
49                 
50             assertThat(receivedProduct.getDescription(), is(expected.getDescription()));
51             assertThat(receivedProduct.getName(), is(expected.getName()));
52
53         }
54
55 }