Skip to content

Commit 2c283a5

Browse files
committed
Update test
1 parent 334fca0 commit 2c283a5

File tree

5 files changed

+75
-11
lines changed

5 files changed

+75
-11
lines changed

src/main/java/com/example/integrationtestspringkafka/config/ConsumerOfExampleDTOConfig.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ ConcurrentKafkaListenerContainerFactory<String, ExampleDTO> kafkaListenerContain
3232
public ConsumerFactory<String, ExampleDTO> consumerFactory() {
3333
Map<String, Object> props = new HashMap<>();
3434
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
35-
props.put(ConsumerConfig.GROUP_ID_CONFIG, "consumer_group_id_eampledto");
3635

3736
return new DefaultKafkaConsumerFactory<>(props, new StringDeserializer(), new JsonDeserializer<>(ExampleDTO.class, false));
3837
}

src/main/java/com/example/integrationtestspringkafka/service/ConsumerService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ public class ConsumerService {
2323
*
2424
* @param exampleDTO
2525
*/
26-
@KafkaListener(topics = "TOPIC_EXAMPLE")
27-
public void consumeExampleDTO(ExampleDTO exampleDTO) {
26+
@KafkaListener(topics = "TOPIC_EXAMPLE", groupId = "consumer_example_dto")
27+
public void consumeExampleDTO(ExampleDTO exampleDTO) throws Exception {
2828
log.info("Received from topic=TOPIC_EXAMPLE ExampleDTO={}", exampleDTO);
29-
3029
exampleRepository.save(convertToExampleEntity(exampleDTO));
31-
log.info("saved in database {}", exampleDTO);
30+
throw new Exception("cannot be done");
31+
// log.info("saved in database {}", exampleDTO);
3232
}
3333

3434
/**

src/test/java/com/example/integrationtestspringkafka/IntegrationtestspringkafkaApplicationTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
import org.junit.jupiter.api.extension.ExtendWith;
55
import org.springframework.boot.test.context.SpringBootTest;
66
import org.springframework.kafka.test.context.EmbeddedKafka;
7+
import org.springframework.test.annotation.DirtiesContext;
78
import org.springframework.test.context.junit.jupiter.SpringExtension;
89

910
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1011

1112

1213
@ExtendWith(SpringExtension.class)
1314
@SpringBootTest
14-
@EmbeddedKafka(topics = {"TOPIC_EXAMPLE"})
15+
@DirtiesContext
16+
@EmbeddedKafka(topics = {"TOPIC_EXAMPLE", "TOPIC_EXAMPLE_EXTERNE"})
1517
class IntegrationtestspringkafkaApplicationTests {
1618

1719
@Test
Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,82 @@
11
package com.example.integrationtestspringkafka.service;
22

3+
import com.example.integrationtestspringkafka.dto.ExampleDTO;
4+
import com.example.integrationtestspringkafka.repository.ExampleRepository;
5+
import org.apache.kafka.clients.producer.KafkaProducer;
6+
import org.apache.kafka.clients.producer.Producer;
7+
import org.apache.kafka.clients.producer.ProducerRecord;
8+
import org.apache.kafka.common.serialization.StringSerializer;
9+
import org.awaitility.Durations;
310
import org.junit.jupiter.api.Test;
411
import org.junit.jupiter.api.extension.ExtendWith;
12+
import org.springframework.beans.factory.annotation.Autowired;
513
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
15+
import org.springframework.kafka.core.ProducerFactory;
16+
import org.springframework.kafka.support.serializer.JsonSerializer;
17+
import org.springframework.kafka.test.EmbeddedKafkaBroker;
618
import org.springframework.kafka.test.context.EmbeddedKafka;
19+
import org.springframework.kafka.test.utils.KafkaTestUtils;
20+
import org.springframework.test.annotation.DirtiesContext;
721
import org.springframework.test.context.junit.jupiter.SpringExtension;
822

9-
import static org.junit.jupiter.api.Assertions.assertFalse;
23+
import java.util.Map;
24+
25+
import static org.awaitility.Awaitility.await;
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
1027

1128
@ExtendWith(SpringExtension.class)
1229
@SpringBootTest
13-
@EmbeddedKafka(topics = { "TOPIC_EXAMPLE" })
30+
@DirtiesContext
31+
@EmbeddedKafka(topics = {"TOPIC_EXAMPLE", "TOPIC_EXAMPLE_EXTERNE"})
1432
public class ConsumerServiceIntegrationTest {
1533

34+
private static final String TOPIC_EXAMPLE = "TOPIC_EXAMPLE";
35+
36+
@Autowired
37+
private EmbeddedKafkaBroker embeddedKafkaBroker;
38+
39+
@Autowired
40+
private ExampleRepository exampleRepository;
41+
42+
public ExampleDTO mockExampleDTO(String name, String description) {
43+
ExampleDTO exampleDTO = new ExampleDTO();
44+
exampleDTO.setDescription(description);
45+
exampleDTO.setName(name);
46+
return exampleDTO;
47+
}
48+
49+
/**
50+
* We verify the output in the topic. But aslo in the database.
51+
*/
1652
@Test
17-
public void itShouldConsumeExampleDTO() {
18-
// TODO
53+
public void itShould_ConsumeCorrectExampleDTO_to_TOPIC_EXAMPLE_and_should_saveCorrectExampleEntity() {
1954
// GIVEN
55+
ExampleDTO exampleDTO = mockExampleDTO("Un nom 2", "Une description 2");
56+
// simulation consumer
57+
Map<String, Object> producerProps = KafkaTestUtils.producerProps(embeddedKafkaBroker);
58+
Producer<String, ExampleDTO> producerTest = new KafkaProducer(producerProps, new StringSerializer(), new JsonSerializer<ExampleDTO>());
59+
60+
ProducerFactory producerFactory = new DefaultKafkaProducerFactory<String, ExampleDTO>(producerProps, new StringSerializer(), new JsonSerializer<ExampleDTO>());
61+
//Producer<String, ExampleDTO> producerTest = producerFactory.createProducer();
62+
ProducerRecord<String, ExampleDTO> producerRecord = new ProducerRecord<String, ExampleDTO>(TOPIC_EXAMPLE,"key", exampleDTO);
63+
// KafkaTemplate<String, ExampleDTO> template = new KafkaTemplate<>(producerFactory);
64+
/// template.setDefaultTopic(TOPIC_EXAMPLE);
65+
// template.send(producerRecord);
2066
// WHEN
67+
producerTest.send( new ProducerRecord<String, ExampleDTO>(TOPIC_EXAMPLE,"", exampleDTO));
68+
producerTest.close();
2169
// THEN
22-
assertFalse(false);
70+
// we must have 1 entity inserted
71+
// We cannot predict when the insertion into the database will occur. So we wait until the value is present. Thank to Awaitility.
72+
// List<ExampleEntity> exampleEntityList = exampleRepository.findAll();
73+
await().atLeast(Durations.ONE_MINUTE).untilAsserted(() -> {
74+
assertEquals(1, exampleRepository.findAll().size());
75+
76+
/// ExampleEntity firstEntity = exampleEntityList.get(0);
77+
78+
//// assertEquals(exampleDTO.getDescription(), firstEntity.getDescription());
79+
// assertEquals(exampleDTO.getName(), firstEntity.getName());
80+
});
2381
}
2482
}

src/test/java/com/example/integrationtestspringkafka/service/ProducerServiceIntegrationTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.springframework.kafka.test.EmbeddedKafkaBroker;
1717
import org.springframework.kafka.test.context.EmbeddedKafka;
1818
import org.springframework.kafka.test.utils.KafkaTestUtils;
19+
import org.springframework.test.annotation.DirtiesContext;
1920
import org.springframework.test.context.junit.jupiter.SpringExtension;
2021

2122
import java.util.Map;
@@ -24,6 +25,8 @@
2425

2526
@ExtendWith(SpringExtension.class)
2627
@SpringBootTest
28+
@DirtiesContext
29+
2730
@EmbeddedKafka(topics = {"TOPIC_EXAMPLE", "TOPIC_EXAMPLE_EXTERNE"})
2831
public class ProducerServiceIntegrationTest {
2932
private static final String TOPIC_EXAMPLE_EXTERNE = "TOPIC_EXAMPLE_EXTERNE";
@@ -66,6 +69,8 @@ public void itShould_ProduceCorrectExampleDTO_to_TOPIC_EXAMPLE_EXTERNE() {
6669

6770
assertEquals("Une description", valueReceived.getDescription());
6871
assertEquals("Un nom", valueReceived.getName());
72+
73+
consumerServiceTest.close();
6974
}
7075

7176
}

0 commit comments

Comments
 (0)