我必须为使用 Zookeeper 获取锁的方法创建一个单元测试用例,并使用 CompletableFuture 处理数据以下是我迄今为止尝试过的测试用例:import org.junit.Test;import org.junit.runner.RunWith;import org.mockito.InjectMocks;import org.mockito.Mock;import org.mockito.Mockito;import org.mockito.MockitoAnnotations;import org.mockito.junit.MockitoJUnitRunner;import java.util.concurrent.ExecutionException; @RunWith(MockitoJUnitRunner.class)public class SampleJobTest { @InjectMocks private SampleJob sampleJob = new SampleJob(); @Mock private ConfigurationsIntegrationService configurationsIntegrationService; @Mock private MessageService messageService; @Mock private LockingExecutorProcessor<ConfigurationsIntegrationModel> lockingExecutorProcessor; @Test public void testSampleJob() throws ExecutionException, InterruptedException { Mockito.doCallRealMethod().when(lockingExecutorProcessor).process(Mockito.any(), Mockito.any(), Mockito.any()); Mockito.doCallRealMethod().when(configurationsIntegrationService).getRecords(Mockito.any()); Mockito.doCallRealMethod().when(messageService).getMessagesList(Mockito.any()); sampleJob.executeJob(); }}当我调试代码时,它会在 LockingExecutorProcessor 的 CompletableFuture.runAsync 行处中断并抛出空指针错误;原因是distributedLockProcessor对象为null;我们如何模拟它以及如何连接到测试 Zookeeper 服务器而不是原始服务器以确保锁定正常工作
添加回答
举报
0/150
提交
取消