首页 > 解决方案 > 回调 Junit 不适用于 ExecutorService

问题描述

我有下面的课程很好。我在下面写了Junit objectMapper.writeValueAsString(producerRecord.value()。我的 Junit 开始正常工作

  1. 如果我删除执行程序服务提交,我的 Junit 开始正常工作。
  2. 或者,如果ObjectMapper m = new ObjectMapper();m.writeValueAsString(e);我奇怪地添加了我的 Junit,即使使用 ExecutorService ,我的测试也开始正常工作。我不明白为什么该解决方案有效。

我怎样才能使 Junitwork 与 ExecutorService 提交和没有第​​二点“黑客:

private final ExecutorService executorService = Executors.newSingleThreadExecutor();

public void sendMessage(List<Employee> empList) {

    ObjectMapper objectMapper = new ObjectMapper();
    

    for (Employee emp : empList) {

        ListenableFuture<SendResult<String, Employee>> listenableFuture = kafkaTemplate.send(topic,emp );

        listenableFuture.addCallback(new ListenableFutureCallback<SendResult<String, Employee>>() {

            @Override
            public void onSuccess(SendResult<String, Employee> result) {
                
                
                executorService.submit(() -> saveResultInDatabase(result.getProducerRecord());

            }

            //onFailure ommiteed for brevity

            private void saveResultInDatabase(ProducerRecord<String, Employee> producerRecord)
                EmpObj e= new EmpObj();

                e.setSalary(producerRecord.value().getSalary());

                
                try {
                    System.out.println(" in here 1"); // JUNIT  reaches here and stops
                     // objectMapper.writeValueAsString(producerRecord.value() is not run and doesn't throw any error 
                    e.setMsg(objectMapper.writeValueAsString(producerRecord.value()));
                    System.out.println(" in here 2"); // JUNIT never reaches here

                } catch (Exception e) {
                    // JUNIT never reaches here
                    System.out.println(" in here 3");
                    
                    

                }
                // JUNIT never reaches here
                System.out.println(" in here 4");

我的Junit在这里

Employee e = new Employee();
        List<Employee> employeeList = new ArrayList<>(); 
        employeeList .add(e);
        
        
        ListenableFuture<SendResult<String, Employee>> responseFuture = mock(ListenableFuture.class);
        SendResult<String, Employee> sendResult = mock(SendResult.class);
         ProducerRecord<String, Employee> producerRecord = mock(ProducerRecord.class);
         
         when(kafkaTemplate.send(null,e)).thenReturn(responseFuture);
            when(sendResult.getProducerRecord()).thenReturn(producerRecord);
            when(producerRecord.value()).thenReturn(e);
            
            doAnswer(invocationOnMock -> {
                ListenableFutureCallback<SendResult<String, Employee>> listenableFutureCallback = invocationOnMock.getArgument(0);
                listenableFutureCallback.onSuccess(sendResult);
               
                return null;
            }).when(responseFuture).addCallback(any(ListenableFutureCallback.class));
            
            myKafkaService.sendMessage(employeeList); //invocation to my real method

标签: javajunitapache-kafkamockitospring-kafka

解决方案


推荐阅读