我的属性如下:
@Component
@Validated
@ConfigurationProperties("kafka")
public class KafkaProperties {
@NotEmpty
String broker;
@NotEmpty
String groupId;
@NotEmpty
String resetOffset = "latest";
@NotEmpty
String topic;
}
我的application.properties如:
kafka.broker=localhost:9092
kafka.groupid=pdp-group
kafka.offset-reset=latest
kafka.topic=pdp-product-fragment
测试是:
@ExtendWith(SpringExtension.class)
@Import({ReactiveKafkaConsumerTest.TestConfig.class})
@PropertySource("classpath:application.properties")
class ReactiveKafkaConsumerTest {
@RegisterExtension
static final SharedKafkaTestResource sharedKafkaTestResource = new SharedKafkaTestResource();
static class TestConfig extends KafkaConfig {
Consumer<ReceiverRecord<String, String>> mockConsumer = mock(Consumer.class);
@Override
List<String> topics() {
String topicName = Integer.toHexString(Math.abs(new Random().nextInt()));
var kafkaTestUtils = sharedKafkaTestResource.getKafkaTestUtils();
kafkaTestUtils.createTopic(topicName, 4, (short) 1);
return asList(topicName);
}
@Override
public Map<String, Object> consumerProps() {
Map<String, Object> props = super.consumerProps();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, sharedKafkaTestResource.getKafkaConnectString());
props.put(ConsumerConfig.GROUP_ID_CONFIG, "group");
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
return props;
}
@Override
public Supplier<Consumer<ReceiverRecord<String, String>>> consumerSupplier() {
return () -> mockConsumer;
}
}
@Autowired
List<String> topics;
@Autowired
TestConfig testConfig;
@Test
@DirtiesContext
public void testReceiveMessages() {
var kafkaTestUtils = sharedKafkaTestResource.getKafkaTestUtils();
IntStream.range(0, 4)
.forEach(i -> kafkaTestUtils.produceRecords(4, topics.get(0), i));
ArgumentCaptor<ReceiverRecord<String, String>> captor = ArgumentCaptor.forClass(ReceiverRecord.class);
verify(testConfig.mockConsumer, timeout(10000).times(16))
.accept(captor.capture());
var recordsPerPartition = captor.getAllValues().stream()
.collect(Collectors.groupingBy(r -> r.partition()));
assertThat(recordsPerPartition.keySet(), containsInAnyOrder(0, 1, 2, 3));
assertEquals(16, recordsPerPartition.values().stream().mapToInt(l -> l.size()).sum());
}
}
它失败并显示:
Caused by: org.springframework.boot.context.properties.bind.validation.BindValidationException: Binding validation errors on kafka
- Field error in object 'kafka' on field 'broker': rejected value [null]; codes [NotEmpty.kafka.broker,NotEmpty.broker,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [kafka.broker,broker]; arguments []; default message [broker]]; default message [must not be empty]
- Field error in object 'kafka' on field 'topic': rejected value [null]; codes [NotEmpty.kafka.topic,NotEmpty.topic,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [kafka.topic,topic]; arguments []; default message [topic]]; default message [must not be empty]
- Field error in object 'kafka' on field 'groupId': rejected value [null]; codes [NotEmpty.kafka.groupId,NotEmpty.groupId,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [kafka.groupId,groupId]; arguments []; default message [groupId]]; default message [must not be empty]
我尝试了很多类似的事情:
春季启动版本为:2.2.2。发布
有人有主意吗?
答案 0 :(得分:0)
将属性或yaml文件绑定到POJO是Spring Boot功能Loading YAML,因此,如果要在测试期间使用它,则需要使用@SpringBootTest加载测试应用程序上下文。而且我建议使用 @Configuration和@Configurationproperties。有关更多信息,请参见here
注意:如果我们不在POJO中使用@Configuration,那么我们需要在主Spring应用程序类中添加@EnableConfigurationProperties(ConfigProperties.class)来将属性绑定到POJO: