Java 如何修复';无效的URL';在Spring测试中创建嵌入式卡夫卡时

Java 如何修复';无效的URL';在Spring测试中创建嵌入式卡夫卡时,java,spring-kafka,Java,Spring Kafka,我正在尝试为一段将数据发送到Kafka主题的代码构建一个测试,我正在阅读一个看起来非常简单的示例,但是当我尝试运行测试时,它失败地抛出以下错误:“bootstrap.servers:spring.embedded.Kafka.brokers中的无效url” URL“spring.embedded.kafka.brokers”我是从文档中获得的,但我现在找不到链接源。这就是我迄今为止所尝试的: @RunWith(MockitoJUnitRunner.class) @SpringBootTest @

我正在尝试为一段将数据发送到Kafka主题的代码构建一个测试,我正在阅读一个看起来非常简单的示例,但是当我尝试运行测试时,它失败地抛出以下错误:“bootstrap.servers:spring.embedded.Kafka.brokers中的无效url”

URL“spring.embedded.kafka.brokers”我是从文档中获得的,但我现在找不到链接源。这就是我迄今为止所尝试的:

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
@DirtiesContext
@TestPropertySource({"classpath:application-test.properties"})
public class PublishCustomerServiceImplTest {

    private String bootstrapServers = "spring.embedded.kafka.brokers";

    private static final String TOPIC = "TopicName";

    public Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.RETRIES_CONFIG, 3);
        props.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, "org.apache.kafka.clients.producer.internals.DefaultPartitioner");
        return props;
    }

    public ProducerFactory<String, CustomerPublishRequest> producerFactory() {
        return new DefaultKafkaProducerFactory<>(producerConfigs());
    }

    public KafkaTemplate<String, CustomerPublishRequest> kafkaTemplate() {
        return new KafkaTemplate<>(producerFactory());
    }

    @ClassRule
    public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, true, TOPIC);

    @Test
    public void publishCustomerTest() throws Exception {

        KafkaTemplate<String, CustomerPublishRequest> kafkaTemplate = kafkaTemplate();

        ListenableFuture<SendResult<String, CustomerPublishRequest>> future = kafkaTemplate.send(TOPIC, CustomerRequestDummy.getCustomer());
        SendResult<String, CustomerPublishRequest> sendResult = future.get();
        long offset = sendResult.getRecordMetadata().offset();

    }
}
@RunWith(MockitoJUnitRunner.class)
@春靴测试
@肮脏的环境
@TestPropertySource({“classpath:application test.properties”})
公共类PublishCustomerServiceImplTest{
私有字符串bootstrapserver=“spring.embedded.kafka.brokers”;
私有静态最终字符串TOPIC=“TopicName”;
公共地图产品配置(){
Map props=newhashmap();
put(ProducerConfig.BOOTSTRAP\u server\u CONFIG,bootstrapserver);
put(ProducerConfig.KEY\u SERIALIZER\u CLASS\u CONFIG,StringSerializer.CLASS);
put(ProducerConfig.VALUE\u SERIALIZER\u CLASS\u CONFIG,StringSerializer.CLASS);
props.put(ProducerConfig.RETRIES\u CONFIG,3);
put(ProducerConfig.PARTITIONER\u CLASS\u CONFIG,“org.apache.kafka.clients.producer.internals.DefaultPartitioner”);
返回道具;
}
公共生产工厂生产工厂(){
返回新的DefaultKafkaProducerFactory(producerConfigs());
}
公共卡夫卡模板卡夫卡模板(){
返回新的卡夫卡模板(producerFactory());
}
@阶级规则
public static EmbeddedKafkaRule embeddedKafka=new EmbeddedKafkaRule(1,true,TOPIC);
@试验
public void publishCustomerTest()引发异常{
KafkaTemplate KafkaTemplate=KafkaTemplate();
ListenableFuture=kafkaTemplate.send(主题,CustomerRequestDummy.getCustomer());
SendResult SendResult=future.get();
long offset=sendResult.getRecordMetadata().offset();
}
}

任何帮助都将不胜感激。

错误消息很清楚,这是
spring.embedded.kafka.brokers
无效的引导服务器URL,请从
EmbeddedKafkaRule

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
@DirtiesContext
@TestPropertySource({"classpath:application-test.properties"})
public class PublishCustomerServiceImplTest {

private static final String TOPIC = "TopicName";

 @ClassRule
public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, true, TOPIC);

public Map<String, Object> producerConfigs() {
    Map<String, Object> props = new HashMap<>();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, embeddedKafka.getEmbeddedKafka().getBrokersAsString());
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put(ProducerConfig.RETRIES_CONFIG, 3);
    props.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, "org.apache.kafka.clients.producer.internals.DefaultPartitioner");
    return props;
}

public ProducerFactory<String, CustomerPublishRequest> producerFactory() {
    return new DefaultKafkaProducerFactory<>(producerConfigs());
}

public KafkaTemplate<String, CustomerPublishRequest> kafkaTemplate() {
    return new KafkaTemplate<>(producerFactory());
}

@Test
public void publishCustomerTest() throws Exception {

    KafkaTemplate<String, CustomerPublishRequest> kafkaTemplate = kafkaTemplate();

    ListenableFuture<SendResult<String, CustomerPublishRequest>> future = kafkaTemplate.send(TOPIC, CustomerRequestDummy.getCustomer());
    SendResult<String, CustomerPublishRequest> sendResult = future.get();
    long offset = sendResult.getRecordMetadata().offset();

    }
}
@RunWith(MockitoJUnitRunner.class)
@春靴测试
@肮脏的环境
@TestPropertySource({“classpath:application test.properties”})
公共类PublishCustomerServiceImplTest{
私有静态最终字符串TOPIC=“TopicName”;
@阶级规则
public static EmbeddedKafkaRule embeddedKafka=new EmbeddedKafkaRule(1,true,TOPIC);
公共地图产品配置(){
Map props=newhashmap();
props.put(ProducerConfig.BOOTSTRAP\u SERVERS\u CONFIG,embeddedKafka.getEmbeddedKafka().getBrokersassString());
put(ProducerConfig.KEY\u SERIALIZER\u CLASS\u CONFIG,StringSerializer.CLASS);
put(ProducerConfig.VALUE\u SERIALIZER\u CLASS\u CONFIG,StringSerializer.CLASS);
props.put(ProducerConfig.RETRIES\u CONFIG,3);
put(ProducerConfig.PARTITIONER\u CLASS\u CONFIG,“org.apache.kafka.clients.producer.internals.DefaultPartitioner”);
返回道具;
}
公共生产工厂生产工厂(){
返回新的DefaultKafkaProducerFactory(producerConfigs());
}
公共卡夫卡模板卡夫卡模板(){
返回新的卡夫卡模板(producerFactory());
}
@试验
public void publishCustomerTest()引发异常{
KafkaTemplate KafkaTemplate=KafkaTemplate();
ListenableFuture=kafkaTemplate.send(主题,CustomerRequestDummy.getCustomer());
SendResult SendResult=future.get();
long offset=sendResult.getRecordMetadata().offset();
}
}