【问题标题】:How to write Junit for if else condition under thrown exceptions from rest template如何在rest模板抛出的异常下为if else条件编写Junit
【发布时间】:2021-09-20 17:58:54
【问题描述】:

我是 Junit 的新手。我阅读了 Mockito 的基础知识。我无法弄清楚如何为测试代码覆盖率编写 Junit。 我正在使用休息模板调用来接收已发送请求的响应对象。 我在这里抛出异常(HttpStatusCodeException)下执行一些逻辑。 以下是我的代码:

public void processData(Request data, HttpHeaders headers, String priority) throws JsonProcessingException {

    try {

        HttpEntity<Request> reqEntity = new HttpEntity<>(data, headers);
        ResponseEntity<Response> helloResponse = restTemplate.postForEntity(someendpoint, reqEntity, Response.class);
        if (priority.equals("low")) {
            func1();
        } else {
            func2();
        }

    } catch (HttpStatusCodeException ex) {

        if (ex.getStatusCode().equals(HttpStatus.SERVICE_UNAVAILABLE) && priority.equals("low")) {

            func3();

        } else if (ex.getStatusCode().equals(HttpStatus.SERVICE_UNAVAILABLE) && priority.equals("high")) {

            func4();

        }

        else if (ex.getStatusCode().equals(HttpStatus.FORBIDDEN) && priority.equals("low")) {

            func5();

        }

        else if (ex.getStatusCode().equals(HttpStatus.FORBIDDEN) && priority.equals("high")) {

            func6();

        }

        else {

            ObjectMapper mapper = new ObjectMapper();
            Response response = mapper.readValue(ex.getResponseBodyAsString(), Response.class);
            if (priority.equals("low")) {
                func7();
            } else {
                func8();
            }
            
        }

    }
}

}

得到一些帮助真的很有帮助。

【问题讨论】:

    标签: spring-boot junit mockito junit4 resttemplate


    【解决方案1】:

    如下操作。

    @Test
    public void test1(){
    
    Mockito.when(restTemplate.postForEntity(ArgumentMatchers.any(), ArgumentMatchers.any, ArgumentMatchers.<Class<Response>>any())).thenReturn(new ResponseEntity<>(new Response(), HttpStatus.ok));
      //do your call and assertion
      //processData( data,  headers,  priority)
    }
    

    测试异常覆盖

      @Test
        public void testException1(){
        
        doThrow(HttpClientErrorException(HttpStatus.SERVICE_UNAVAILABLE).when(restTemplate).postForEntity(ArgumentMatchers.any(), ArgumentMatchers.any, ArgumentMatchers.<Class<Response>>any());
        //do your call and assertion
        //processData( data,  headers,  "low")
        }
        
        
        
        @Test
        public void testException2(){
             doThrow(HttpClientErrorException(HttpStatus.FORBIDDEN).when(restTemplate).postForEntity(ArgumentMatchers.any(), ArgumentMatchers.any, ArgumentMatchers.<Class<Response>>any());
        //do your call and assertion
        //processData( data,  headers,  "low")
        }
    
      
    
       
    
     @Test
     public void testException3(){ 
             doThrow(HttpClientErrorException(HttpStatus.SERVICE_UNAVAILABLE).when(restTemplate).postForEntity(ArgumentMatchers.any(), ArgumentMatchers.any, ArgumentMatchers.<Class<Response>>any());
                //do your call and assertion
                //processData( data,  headers,  "high")
                }
                
                
                
     @Test
     public void testException4(){
                
                doThrow(HttpClientErrorException(HttpStatus.FORBIDDEN).when(restTemplate).postForEntity(ArgumentMatchers.any(), ArgumentMatchers.any, ArgumentMatchers.<Class<Response>>any());
                //do your call and assertion
                //processData( data,  headers,  "high")
                }
    

    【讨论】:

    • 你有没有从上面得到解决方案。
    • 例如 testException2(),你可以测试像通过优先级一样低,并创建另一个测试方法作为 testException2(),并像我们可以覆盖的那样通过高优先级
    • 我知道它会再次分解为两个不同的测试用例。我是否需要链接到现有的 throw 类以通过优先级?如果您可以将其包含在答案中,那就太好了。
    • 现在在上面查看
    • 嗯,但如果使用看到我的函数调用没有 arg。我普遍关心的是,当条件像 and(&&) case 时,我们不能将两者结合起来吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多