簡體   English   中英

多模塊 Maven 項目與 Spring 的集成測試

[英]Integration testing in multi module Maven project with Spring

我有一個包含多個模塊(父、服務、updater1、updater2)的多模塊 maven 項目。 @SpringBootApplication 在“服務”模塊中,其他沒有工件。

'updater1' 是一個具有 Kafka 監聽器和 http 客戶端的模塊,當接收到 kafka 事件時,會向外部 API 發起請求。 我想用testcontainers在這個模塊中創建集成測試,所以我創建了容器和一個 Kafka 生產者來向我的消費者發送一個 KafkaTemplate。

我的問題是 Kafka 生產者正在自動裝配到 null,因此測試會引發 NullPointerException。 我想應該是Spring的配置問題,但是找不到問題。 你能幫助我嗎? 謝謝!

這是我的測試 class:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {KafkaConfiguration.class, CacheConfiguration.class, ClientConfiguration.class})
public class InvoicingTest {

@ClassRule
public static final Containers containers = Containers.Builder.aContainer()
        .withKafka()
        .withServer()
        .build();

private final MockHttpClient mockHttpClient =
        new MockHttpClient(containers.getHost(SERVER),
                containers.getPort(SERVER));

@Autowired
private KafkaEventProducer kafkaEventProducer;

@BeforeEach
@Transactional
void setUp() {
    mockHttpClient.reset();
}

@Test
public void createElementSuccesfullResponse() throws ExecutionException, InterruptedException, TimeoutException {

    mockHttpClient.whenPost("/v1/endpoint")
            .respond(HttpStatusCode.OK_200);

    kafkaEventProducer.produce("src/test/resources/event/invoiceCreated.json");

    mockHttpClient.verify();

}

這是事件制作者:

@Component
public class KafkaEventProducer {

private final KafkaTemplate<String, String> kafkaTemplate;

private final String topic;

@Autowired
KafkaInvoicingEventProducer(KafkaTemplate<String, String> kafkaTemplate,
                            @Value("${kafka.topic.invoicing.name}") String topic){
    this.kafkaTemplate = kafkaTemplate;
    this.topic = topic;
}

public void produce(String event){
    kafkaTemplate.send(topic, event);
}

}

您還沒有詳細說明KafkaEventProducer是如何實現的(它是@Component嗎?),您的測試 class 也沒有使用@SpringBootTest和運行器@RunWith進行注釋。

查看此示例,使用 Apache KakfaProducer:

import org.apache.kafka.clients.producer.KafkaProducer;

public void sendRecord(String topic, String event) {
        try (KafkaProducer<String, byte[]> producer = new KafkaProducer<>(producerProps(bootstrapServers, false))) {
            send(producer, topic, event);
        } 
    }

在哪里

public void send(KafkaProducer<String, byte[]> producer, String topic, String event) {
    try {
      ProducerRecord<String, byte[]> record = new ProducerRecord<>(topic, event.getBytes());
      producer.send(record).get();
    } catch (InterruptedException | ExecutionException e) {
        fail("Not expected exception: " + e.getMessage());
    }
}


protected Properties producerProps(String bootstrapServer, boolean transactional) {
        Properties producerProperties = new Properties();
        producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
        producerProperties.put(KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        producerProperties.put(VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class.getName());
        if (transactional) {
            producerProperties.put(TRANSACTIONAL_ID_CONFIG, "my-transactional-id");
        }
            
        return producerProperties;
    }

bootstrapServers取自 kafka 容器:

KafkaContainer kafka = new KafkaContainer();
kafka.start();
bootstrapServers = kafka.getBootstrapServers();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM