我必須為使用 Zookeeper 獲取鎖的方法創(chuàng)建一個(gè)單元測(cè)試用例,并使用 CompletableFuture 處理數(shù)據(jù)以下是我迄今為止嘗試過(guò)的測(cè)試用例: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(); }}當(dāng)我調(diào)試代碼時(shí),它會(huì)在 LockingExecutorProcessor 的 CompletableFuture.runAsync 行處中斷并拋出空指針錯(cuò)誤;原因是distributedLockProcessor對(duì)象為null;我們?nèi)绾文M它以及如何連接到測(cè)試 Zookeeper 服務(wù)器而不是原始服務(wù)器以確保鎖定正常工作
1 回答

Qyouu
TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊
這個(gè)做完了?,F(xiàn)在測(cè)試用例運(yùn)行良好。我使用了 curator-test 依賴項(xiàng),并使用反射來(lái)模擬私有對(duì)象。
添加回答
舉報(bào)
0/150
提交
取消