我有一个MicroService发送到另一个应该使用的微服务消息。
因此,kafka吐露起作用,一切都正常,但我需要为这段代码创建一个集成测试,我不知道如何实现。
我的KafkaConsumer.Class涂上了成分阳极:
private static final Logger logger = LoggerFactory.getLogger(KafkaReactionConsumerMessageComponent.class);
private final ReactionsService reactionsService;
public KafkaReactionConsumerMessageComponent(ReactionsService reactionsService) {
this.reactionsService = reactionsService;
}
@KafkaListener(topics = "reaction-topic", clientIdPrefix = "string", groupId = "magpie-trending")
public void consumingReactionMessages(ConsumerRecord<String, String> cr,
@Payload String payload){
logger.info("[JSON] received Payload: {}", payload);
try {
ObjectMapper mapper = new ObjectMapper();
ReactionMessage message = mapper.readValue(payload, ReactionMessage.class);
if(StringUtils.equals("unloved", message.getReactionType())) {
reactionsService.deleteReactionsByUserIdAndPostId(message.getPost().getPostId(), message.getUser().getUserId());
logger.info("Deleted reactions from database with postId: {} and userId: {}", message.getPost().getPostId(), message.getUser().getUserId());
} else {
List<Reaction> reactions = creatReactions(message).stream()
.map(reactionsService::insertReaction).collect(Collectors.toList());
logger.info("Added reactions to database: {}", reactions);
}
} catch (Exception e){
logger.error("Cannot Deserialize payload to ReactionMessage");
}
}我的集成测试是
private static final String TOPIC = "reaction-topic";
private final Logger logger = LoggerFactory.getLogger(KafkaDeletePostConsumerMessageComponent.class);
private final KafkaReactionConsumerMessageComponent kafkaReactionConsumerMessageComponent;
private final EmbeddedKafkaBroker embeddedKafkaBroker;
private Consumer<String, String> consumer;
@SuppressWarnings("SpringJavaAutowiringInspection")
@Autowired
public KafkaReactionMessageConsumerTest(KafkaReactionConsumerMessageComponent kafkaReactionConsumerMessageComponent,
EmbeddedKafkaBroker embeddedKafkaBroker) {
this.kafkaReactionConsumerMessageComponent = kafkaReactionConsumerMessageComponent;
this.embeddedKafkaBroker = embeddedKafkaBroker;
}
@BeforeEach
public void setUp() {
Map<String, Object> configs = new HashMap<>(KafkaTestUtils.consumerProps("consumer", "true", embeddedKafkaBroker));
consumer = new DefaultKafkaConsumerFactory<>(configs, new StringDeserializer(), new StringDeserializer()).createConsumer();
consumer.subscribe(Collections.singleton(TOPIC));
consumer.poll(Duration.ZERO);
}
@AfterEach
public void tearDown() {
consumer.close();
}
@Test
public void shoudlConsumeAndInsertInDatabaseReactionDomain() {
ReactionMessage reactionMessage = new ReactionMessage(new PostMessage("1", Set.of("a", "b", "c")),
new UserMessage("2"), LocalDateTime.now().toString(), "loved");
Map<String, Object> configs = new HashMap<>(KafkaTestUtils.producerProps(embeddedKafkaBroker));
Producer<String, String> producer = new DefaultKafkaProducerFactory<>(configs, new StringSerializer(), new StringSerializer()).createProducer();
producer.send(new ProducerRecord<>(TOPIC, "1", reactionMessage.toString()));
producer.flush();
assertEquals(3, mongoTemplate.getCollection("reactions").countDocuments());
}AbstractClass:
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
@EmbeddedKafka(brokerProperties={
"log.dir=out/embedded-kafka"
})
public abstract class AbstractMongoEmbeddedTest {
@Autowired
private static MongodExecutable mongodExecutable;
@Autowired
protected MongoTemplate mongoTemplate;
@BeforeEach
private void dropPostCollection(){
mongoTemplate.dropCollection(Reaction.class);
}发布于 2020-01-20 13:16:05
由于您使用的是嵌入式Kafka broker,所以您只需在集成测试中生成/使用所需的主题。
消费
消费可以通过一个简单的jUnit规则来完成。用于此目的的规则可以找到这里。请随意使用它。
您可以像这样使用它来断言已消费的消息:
assertThat(kafkaConsumeMessagesRule.pollMessage()).isEqualTo("your-expected-message");生产
为了生成消息,您可以简单地在集成测试中连接org.springframework.kafka.core.KafkaTemplate,并将消息发送到给定的主题。
https://stackoverflow.com/questions/59823798
复制相似问题