簡體   English   中英

如何使用 Mockito 模擬 Do-While 循環?

[英]How do I mock Do-While Loop using Mockito?

//Below are the methods  
public Apple getAppleByNameVersion(String server, String appleName, String version) throws SSLException {

    List<Apple> allAppleList = getAllApplesByServer(server);

    for(int i = 0; i < allAppleList.size(); i++){
        if(allAppleList.get(i).getName().equals(appleName) && allAppleList.get(i).getEntityVersion().equals(version)){
            return allAppleList.get(i);
        }
    }
    return null;
}

public List<Apple> getAllApplesByServer(String server) throws SSLException {
    List<Apple> allAppleList = new ArrayList<>();
    List<Apple> appleListByPage = new ArrayList<>();
    int page = 1;
    AppleServerEnum appleServerEnum = AppleServerEnum.getByServer(server);
    String urlApi = appleServerEnum.getUrl();
    do{
        Flux<Apple> apple = webClient.get()
            .uri(urlApi + "?page=" + page)
            .header(HEADER_NAME, HEADER_VALUE)
            .retrieve()
            .bodyToFlux(Strategy.class);
        appleListByPage = apple.collectList().block();
        allAppleList.addAll(appleListByPage);
        page++;

    }while(!appleListByPage.isEmpty());

    return allappleList;
}

//Below is the test
@Test
void get_withSuccess() throws FileNotFoundException, SSLException, JsonProcessingException {
    Apple apple = new Apple();
    Apple apple1 = new Apple();
    List<Apple> allAppleList = new ArrayList<>();
    List<Apple> appleListByPage = Arrays.asList(apple, apple1);
    int page = 1;

    String farmUri = "https://podpc1y-dr3-core.pod-fx.us.dell.com/abre-admin-api/abre/adminapi/v7/strategyscripts";

    Mono<List> mono = mock(Mono.class);


    when(webClientMock.get()).thenReturn(requestHeadersUriSpec);
    when(requestHeadersUriSpec.uri(anyString())).thenReturn(requestBodyMock);
    when(requestBodyMock.header(any(), any())).thenReturn(requestBodyMock);
    when(requestBodyMock.retrieve()).thenReturn(responseMock);
    when(responseMock.bodyToFlux(Strategy.class)).thenReturn(Flux.just(new apple()));
    strategyListByPage=mono.block();

    // act
    appleService.getAllapplesByServer("farm");

    // assert
    verify(webClientMock).get();
    // TODO: add more assertions
}

你不能模擬一個循環。 在您的情況下模擬 webClient。 還有一件事,您正在使用 block()。 這是一個非常糟糕的做法。 您不應該阻止響應式代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM