gpt寫的

一曲微茫發表於2024-11-23

restoreSnapshotIndices 方法編寫單元測試,需要覆蓋以下關鍵邏輯:

  1. 請求引數為空時丟擲異常
  2. 呼叫 restoreSnapshot 方法對每個 SnapShotDTO 的邏輯
  3. 捕獲 IOException 並驗證日誌輸出和錯誤處理邏輯
  4. 正確返回 RestoreSnapshotIndicesResponse 的結果

以下是單元測試程式碼示例(假設使用 JUnit 5 和 Mockito):

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;

class SnapshotServiceImplTest {

    @InjectMocks
    private SnapshotServiceImpl snapshotService;

    @Mock
    private RestHighLevelClient restHighLevelClient;

    @Mock
    private ElasticSearchService elasticSearchService;

    @Mock
    private IndicesRestoreMapper indicesRestoreMapper;

    @Mock
    private IndicesRestoreRecordMapper indicesRestoreRecordMapper;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
    }

    @Test
    void testRestoreSnapshotIndices_NullRequest() {
        RestoreSnapshotIndicesRequest request = null;
        Exception exception = assertThrows(TitanException.class, () -> {
            snapshotService.restoreSnapshotIndices(request);
        });
        assertEquals("所傳引數為空!", exception.getMessage());
    }

    @Test
    void testRestoreSnapshotIndices_EmptySnapshotList() {
        RestoreSnapshotIndicesRequest request = new RestoreSnapshotIndicesRequest();
        request.setSnapShotList(Collections.emptyList());

        Exception exception = assertThrows(TitanException.class, () -> {
            snapshotService.restoreSnapshotIndices(request);
        });
        assertEquals("所傳引數為空!", exception.getMessage());
    }

    @Test
    void testRestoreSnapshotIndices_Success() throws IOException {
        SnapShotDTO snapShotDTO = new SnapShotDTO();
        snapShotDTO.setRepositoryName("repo1");
        snapShotDTO.setSnapshotName("snapshot1");
        snapShotDTO.setIndices(Arrays.asList("index1", "index2"));

        RestoreSnapshotIndicesRequest request = new RestoreSnapshotIndicesRequest();
        request.setSnapShotList(Collections.singletonList(snapShotDTO));

        doNothing().when(snapshotService).restoreSnapshot(
                anyString(), anyString(), anyList(), any(), anyLong()
        );

        RestoreSnapshotIndicesResponse response = snapshotService.restoreSnapshotIndices(request);

        assertNotNull(response);
        assertEquals(0, response.getSuccess());
        verify(snapshotService, times(1)).restoreSnapshot(
                "repo1", "snapshot1", Arrays.asList("index1", "index2"), null, 0L
        );
    }

    @Test
    void testRestoreSnapshotIndices_Failure() throws IOException {
        SnapShotDTO snapShotDTO = new SnapShotDTO();
        snapShotDTO.setRepositoryName("repo1");
        snapShotDTO.setSnapshotName("snapshot1");
        snapShotDTO.setIndices(Arrays.asList("index1", "index2"));

        RestoreSnapshotIndicesRequest request = new RestoreSnapshotIndicesRequest();
        request.setSnapShotList(Collections.singletonList(snapShotDTO));

        doThrow(new IOException("Test exception")).when(snapshotService).restoreSnapshot(
                anyString(), anyString(), anyList(), any(), anyLong()
        );

        RestoreSnapshotIndicesResponse response = snapshotService.restoreSnapshotIndices(request);

        assertNotNull(response);
        assertEquals(-1, response.getSuccess());
        verify(snapshotService, times(1)).restoreSnapshot(
                "repo1", "snapshot1", Arrays.asList("index1", "index2"), null, 0L
        );
    }
}

說明

  1. 使用了 Mockito 來模擬依賴和方法呼叫。
  2. 測試場景包括:
    • 空請求。
    • snapShotList
    • 恢復成功。
    • 恢復失敗丟擲異常。
  3. 驗證了正確的方法呼叫次數和引數。

前提

  • 確保 restoreSnapshotpublic 或者 protected,以便可以透過 Mockito 進行 spymock
  • 配置好測試環境,載入所有依賴類和配置。

相關文章