Home » Java » java – EmbeddedKafkaRule have no brokerListProperty method-Exceptionshub

java – EmbeddedKafkaRule have no brokerListProperty method-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to figure out Spring Kafka. While following reference I found error in this example. There is no brokerListProperty() method in EmbeddedKafkaRule class. How should I refactor the code to make it work?

Code from link above:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyApplicationTests {

    @ClassRule
    public static EmbeddedKafkaRule broker = new EmbeddedKafkaRule(1,
        false, "someTopic")
            .brokerListProperty("spring.kafka.bootstrap-servers");
    }

    @Autowired
    private KafkaTemplate<String, String> template;

    @Test
    public void test() {
        ...
    }

}
How to&Answers:

Together with the @SpringBootTest consider to use an @EmbeddedKafka instead.
That one has a property like:

/**
 * The property name to set with the bootstrap server addresses instead of the default
 * {@value org.springframework.kafka.test.EmbeddedKafkaBroker#SPRING_EMBEDDED_KAFKA_BROKERS}.
 * @return the property name.
 * @since 2.3
 * @see org.springframework.kafka.test.EmbeddedKafkaBroker#brokerListProperty(String)
 */
String bootstrapServersProperty() default "";

The goal for an EmbeddedKafkaRule, when we don’t use Spring in tests at all.
And I agree that we are missing a propagation property from that EmbeddedKafkaRule into a brokerListProperty(). Please, feel free to raise a GH issue and contribute a fix on the matter.

Meanwhile you can workaround it like this:

@ClassRule
public static EmbeddedKafkaRule broker = new EmbeddedKafkaRule(1, false, "someTopic");

@BeforeAll
pubic static void setup() {
    broker.getEmbeddedKafka().brokerListProperty("spring.kafka.bootstrap-servers");
}

Answer:

You can add brokerListProperty like that using KafkaTestUtils

    @Before
  public void setUp() throws Exception {
    // set up the Kafka producer properties
    Map<String, Object> senderProperties =
        KafkaTestUtils.senderProps(
            broker.getEmbeddedKafka().getBrokersAsString());
  }

And you can follow this tutorial it seems pretty correct.