kafka单元测试等待失败

czq61nw1  于 2021-06-04  发布在  Kafka
关注(0)|答案(0)|浏览(241)

我已经编写了一个测试,但在Assert部分,它通知
assert.asserttrue(latch.await(0,timeunit.seconds));
可能是错误的,测试失败。
我怎样才能解决这个问题。或者这个有更好的方法来测试吗?

package az.iba.ms.customer.kafka;

imports *

@RunWith(SpringRunner.class)
@SpringBootTest
@DirtiesContext
@EmbeddedKafka(topics = { "${kafka.topics.customer}" })
@TestPropertySource(properties = { "kafka.bootstrap-servers=${embedded.kafka.brokers}" })
public class ListenerTest {

    @Value("${kafka.topics.customer}")
    private String topic;

    @MockBean
    private EmbeddedKafkaBroker embeddedKafka;

    @Mock
    private CountDownLatch latch;

    @Mock
    private KafkaTemplate<Integer, String> producer;

    @Mock
    CustomerModifyResponseDTO customerModifyResponseDto;

    @Before
    public void setUp() {
        this.producer = buildKafkaTemplate();
        this.producer.setDefaultTopic(topic);

        customerModifyResponseDto = CustomerModifyResponseDTO.builder()
                .fcubsheader(null)
                .fcubsbody(null)
                .build();
    }

    private KafkaTemplate<Integer, String> buildKafkaTemplate() {
        Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka);
        ProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps);
        return new KafkaTemplate<>(pf);
    }

    @Test
    public void listenerShouldConsumeMessages() throws InterruptedException {
        // Given
        producer.sendDefault(1, "Hello world");
        // Then
        Assert.assertTrue(latch.await(0, TimeUnit.SECONDS));

    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题