mockito 什么是Mono类型参数的参数匹配器?

bfrts1fy  于 2022-11-08  发布在  其他
关注(0)|答案(2)|浏览(181)

受试类别:

import java.time.Duration;
import java.time.Instant;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

public class ServiceProcessorImpl implements ServiceProcessor {

    @Autowired
    private WebClient webClient;

    @Override
    public Mono<ServiceResponse> fetchOffersByItemIdAndStoreId(Mono<List<PriceAndOfferRequest>> list) {
        String url = "<some url>";
        Mono<ServiceResponse> response = webClient
                        .post()
                        .uri(url)
                        .headers(httpHeaders -> getHeaders(httpHeaders))
                        .body(list,List.class)
                        .retrieve()
                        .onStatus(HttpStatus::isError, clientResponse -> {
                            log.error("Error on API Calling :  {}", clientResponse.statusCode());
                            return Mono.empty();
                        })
                        .bodyToMono(PnOServiceResponse.class);
        return response;
    }
}

测试用例:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

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

@RunWith(SpringRunner.class)
public class ServiceProcessorImplTest {

    @Mock
    private static WebClient webClientMock;

    @Mock
    private WebClient.RequestHeadersSpec requestHeadersSpecMock;
    @Mock
    private WebClient.RequestHeadersUriSpec requestHeadersUriSpecMock;
    @Mock
    private WebClient.RequestBodySpec requestBodySpecMock;
    @Mock
    private WebClient.RequestBodyUriSpec requestBodyUriSpecMock;
    @Mock
    private WebClient.ResponseSpec responseSpecMock;

    @InjectMocks
    static ServiceProcessorImpl pnOServiceProcessor;

    @Test
    public void test_whenErrorFromService_thenMonoEmpty() {
        Mockito.when(webClientMock.post()).thenReturn(requestBodyUriSpecMock);
        Mockito.when(requestBodyUriSpecMock.uri(Mockito.anyString())).thenReturn(requestBodySpecMock);
        Mockito.when(requestBodySpecMock.headers(Mockito.any())).thenReturn(requestBodySpecMock);
        Mockito.when(requestBodySpecMock.body(Mockito.any(Mono.class), Mockito.any(Class.class))).thenReturn(requestHeadersSpecMock);
        Mockito.when(requestHeadersSpecMock.retrieve()).thenReturn(responseSpecMock);
        Mockito.when(responseSpecMock.onStatus(Mockito.any(), Mockito.any())).thenReturn(responseSpecMock);
        Mockito.when(responseSpecMock.bodyToMono(Mockito.eq(PnOServiceResponse.class))).thenReturn(Mono.empty());

        Mono<List<PriceAndOfferRequest>> reqList = createPriceAndOfferRequestList();
        Mono<PnOServiceResponse> pnOServiceResponseMono = pnOServiceProcessor.fetchOffersByItemIdAndStoreId(reqList);

        StepVerifier.create(pnOServiceResponseMono)
                .expectNextCount(0)
                .verifyComplete();
    }

    private Mono<List<PriceAndOfferRequest>> createPriceAndOfferRequestList() {
        PriceAndOfferRequest pnoRequest = new PriceAndOfferRequest();
        pnoRequest.setItemId("12345");
        pnoRequest.setStoreNumber("store12345");
        List<PriceAndOfferRequest> list = Arrays.asList(pnoRequest);
        return Mono.just(list);
    }

    private Mono<PnOServiceResponse> createMockPnOResponse() {
        PnOServiceResponse pnOServiceResponse = new PnOServiceResponse();
        OfferPriceDTO offerPriceDTO = new OfferPriceDTO();
        PnOItem item = new PnOItem();
        item.setItemId("12345");
        offerPriceDTO.setItems(Arrays.asList(item));
        pnOServiceResponse.setOfferPriceDTO(offerPriceDTO);
        return Mono.just(pnOServiceResponse);
    }
}

fetchOffersByItemIdAndStoreId方法中的webClient上调用.body()时,代码给出NullPointerException。在模拟Mockito.when(requestBodySpecMock.body(Mockito.any(Mono.class), Mockito.any(Class.class))).thenReturn(requestHeadersSpecMock);中对body()的调用时,测试用例中的ArgumentMatcher似乎存在一些问题

nlejzf6q

nlejzf6q1#

您可以使用通用匹配器ArgumentMatchers.any()

polkgigr

polkgigr2#

已解决。
Mockito.any(Object.class)对我起作用了。

Mockito.when(requestBodySpecMock.body(Mockito.any(Object.class), Mockito.any(Class.class))).thenReturn(requestHeadersSpecMock);

相关问题