Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Mockito Kafka侦听器测试用例:无代码覆盖_Java_Spring_Junit_Apache Kafka_Mockito - Fatal编程技术网

Java Mockito Kafka侦听器测试用例:无代码覆盖

Java Mockito Kafka侦听器测试用例:无代码覆盖,java,spring,junit,apache-kafka,mockito,Java,Spring,Junit,Apache Kafka,Mockito,我不熟悉java和Kafka,编写了一个方法,该方法使用Kafka的事件并将映射值传递给另一个类方法。 方法consumeCreatedEvent将使用该事件,并使用objectMapper将string类型的事件映射到java对象中。该对象将作为参数传递给另一个函数 @Component public class EventListener { private final Logger logger = LoggerFactory.getLogger(EventListener.cl

我不熟悉java和Kafka,编写了一个方法,该方法使用Kafka的事件并将映射值传递给另一个类方法。 方法consumeCreatedEvent将使用该事件,并使用objectMapper将string类型的事件映射到java对象中。该对象将作为参数传递给另一个函数

@Component
public class EventListener {

    private final Logger logger = LoggerFactory.getLogger(EventListener.class);

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private EventPublisher eventPublisher;

@KafkaListener(topics = "${someTopic}", groupId = "someGroup")
    public void consumeCreatedEvent(String event) {

        logger.info("Received created event");
        try {
            SmsNotificationCreatedEvent smsNotificationCreatedEvent = objectMapper.readValue(event, SmsNotificationCreatedEvent.class);
            eventPublisher.publishUpdatedIncomingCreatedEvent(smsNotificationCreatedEvent);
        } catch (JsonProcessingException ex) {
            logger.error("Cannot Parse the Incoming Created event", ex);
        }
    }
我正在使用mockito为这个方法编写一个单元测试用例。我的代码覆盖率为零

public class EventListenerTest {

    EventPublisher eventPublisher = mock(EventPublisher.class);

    @Autowired
    private EventListener eventListener;

    @Test
    void whenCallingConsumeCreatedEvent() {
        SmsNotificationCreatedEvent smsNotificationCreatedEventTest = new SmsNotificationCreatedEvent(MESSAGE_ID);
        eventListener.consumeCreatedEvent("This is a string for testing");
        doNothing().when(eventPublisher).publishUpdatedIncomingCreatedEvent(smsNotificationCreatedEventTest);
        verify(eventListener, times(1)).consumeCreatedEvent("This is a string for testing");

    }
对于这个方法测试用例,我得到了NullPointerException。 我的测试用例对吗?或者我需要用其他方法来测试这个函数吗