使用 Spring 在多模块 Maven 项目中进行集成测试
Integration testing in multi module Maven project with Spring
我有一个包含多个模块(parent、service、updater1、updater2)的多模块 maven 项目。
@SpringBootApplication 在 'service' 模块中,其他模块没有工件。
'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();
我有一个包含多个模块(parent、service、updater1、updater2)的多模块 maven 项目。 @SpringBootApplication 在 'service' 模块中,其他模块没有工件。
'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();