在项目中,方法已经作为模块的一部分存在。我正在尝试编写一个测试案例以提高覆盖率。该方法及其关联的类为:

@Component
public class GenericRestTemplate {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    CtmLogger ctmLogger;

    public <R, T> R persistGenericModelsAsync(T requestModel, Class<R> responseModel, String url, String postCookies)
            throws JsonProcessingException {
        ctmLogger.debugObject(ValueConstant.SHOPPING_CART_SERVICE_NAME,"REST call", url);
        HttpMethod method = HttpMethod.POST;
        Class<R> responseType = responseModel;
        ResponseEntity<JsonNode> response = null;
        HttpHeaders headers = getHeaders(postCookies);
        HttpEntity<T> requestEntity = new HttpEntity<>(requestModel, headers);
        response = restTemplate.exchange(url, method, requestEntity, JsonNode.class);
        ctmLogger.debugObject(ValueConstant.SHOPPING_CART_SERVICE_NAME,"response", response);
        List<String> cookies = response.getHeaders().get(ValueConstant.SET_COOKIE);
        ctmLogger.info("test:", cookies.toString());
        JsonNode responseBody = response.getBody();
        for (String cookie : cookies) {
            System.out.println(cookie);
            if (cookie.startsWith(((ValueConstant.TX_CART_ID)))) {
                ((ObjectNode) responseBody).put(ValueConstant.COOKIES, cookie);
            }
        }

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        if ( response.getBody() != null) {
            System.out.println("entered here");
            return objectMapper.treeToValue(response.getBody(), responseType);
        }
        return (R) response.getBody();
    }

    private HttpHeaders getHeaders(String cartIdCookies) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        headers.add(HttpHeaders.COOKIE, cartIdCookies);
        headers.add(HttpHeaders.HOST, "www-sit-g1.dell.com");
        return headers;
    }


现在,我正在尝试为persistGenericModelsAsync()编写一个测试用例。代码如下:

@RunWith(MockitoJUnitRunner.class)
public class GenericRestTemplateTest {

    @InjectMocks
    GenericRestTemplate genericRestTemplate;

    @Mock
    RestTemplate restTemplate;

    @Mock
    CartRequest request;

    @Mock
    HttpEntity<CartRequest> requestEntity;

    @Mock
    ResponseEntity<JsonNode> response;

    @Mock
    HttpHeaders headers;

    @Mock
    List<String> cookies;

    @Mock
    JsonNode responseBody;

    @Mock
    ObjectMapper objectMapper;

    @Mock
    CartResponse cartResponse;

    @Mock
    CtmLogger ctmLogger;

    @Mock
    JsonNode jsonNode;

    @Mock
    ObjectNode objectNode;

    @Before
    public void setup() {
        cookies = Arrays.asList("TxCartIdtest", "test");

    }

    @Test
    public void persistGenericModelsAsync() throws JsonProcessingException {

        when(restTemplate.exchange(Mockito.anyString(), Mockito.eq(HttpMethod.POST), Mockito.any(HttpEntity.class), Mockito.eq(JsonNode.class))).thenReturn(response);
        when(response.getHeaders()).thenReturn(headers);
        List<String> cookiesTest = Arrays.asList("TxCartIdtest", "test");
        when(headers.get(ValueConstant.SET_COOKIE)).thenReturn(cookiesTest);
        when(response.getBody()).thenReturn(jsonNode);
    }


错误的堆栈跟踪为:

org.mockito.exceptions.misusing.UnnecessaryStubbingException:

Unnecessary stubbings detected in test class: GenericRestTemplateTest
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
  1. -> at com.dell.ctm.cart.util.GenericRestTemplateTest.persistGenericModelsAsync(GenericRestTemplateTest.java:81)
  2. -> at com.dell.ctm.cart.util.GenericRestTemplateTest.persistGenericModelsAsync(GenericRestTemplateTest.java:82)
  3. -> at com.dell.ctm.cart.util.GenericRestTemplateTest.persistGenericModelsAsync(GenericRestTemplateTest.java:84)
  4. -> at com.dell.ctm.cart.util.GenericRestTemplateTest.persistGenericModelsAsync(GenericRestTemplateTest.java:85)
Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.


我正在尝试模拟Cookie列表,并尝试查看该方法是否正在执行并覆盖其中的所有语句。但这会引发此不必要的存根异常。我在哪里不必要地打了?头?

最佳答案

@Test
public void persistGenericModelsAsync() throws JsonProcessingException {

    //stubbed - but never used
    when(restTemplate.exchange(Mockito.anyString(), Mockito.eq(HttpMethod.POST), Mockito.any(HttpEntity.class), Mockito.eq(JsonNode.class))).thenReturn(response);

    //stubbed - but never used
    when(response.getHeaders()).thenReturn(headers);
    List<String> cookiesTest = Arrays.asList("TxCartIdtest", "test");
    //stubbed - but never used
    when(headers.get(ValueConstant.SET_COOKIE)).thenReturn(cookiesTest);
    //stubbed - but never used
    when(response.getBody()).thenReturn(jsonNode);
}


很明显,用when()创建的存根在任何地方都不会被使用。
例如,这是您返回响应对象的第一个存根-在此方法的测试执行中,您从未在任何地方使用restTemplate.exchange()调用。

使用strict存根模式时,需要确保-使用创建的任何存根。见more here

07-26 04:47