Mockito 单元测试 RestTemplate [英] Mockito unit testing RestTemplate

查看:52
本文介绍了Mockito 单元测试 RestTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RestTemplate postForEntity 方法将正文发布到端点.我需要帮助使用 Mockito 为我的代码编写测试用例.返回类型为 void,但如果需要测试,可以将其更改为 Typescode.我参考了许多其他文档,但它们非常通用,我尝试使用它们但大多数对我不起作用,因为 request 和返回类型不同..任何建议表示赞赏.谢谢

I am using RestTemplate postForEntity method to post body to an endpoint. I need help with writing test case for my code using Mockito. The return type is void but it can be changed to Types or code if needed to test. I have referred many other documentation but they are very general, I tried using them but most did not work for me as the request and return type are different. . Any suggestions are appreciated. Thank you

这是我的 Java 类

Here is my Java class

    public void postJson(Set<Type> Types){
        try {
            String oneString = String.join(",", Types);
           Map<String, String> requestBody = new HashMap<>();
            requestBody.put("type", oneString);
            JSONObject jsonObject = new JSONObject(requestBody);
            HttpEntity<String> request = new HttpEntity<String>(jsonObject.toString(), null);
ResponseEntity result = restTemplate.exchange(url, HttpMethod.POST,
                    new HttpEntity<>(request, getHttpHeaders()), String.class);

        } 
    }
} 

推荐答案

您正在测试 MyClass 类中的逻辑,因此不应模拟它.RestTemplate 是 MyClass 中的一个依赖项,所以这正是你需要模拟的.一般来说,它在你的测试中应该是这样的:

You are testing the logic inside MyClass class, so you should not mock it. RestTemplate is a dependency inside MyClass, so this is exactly what you need to mock. In general it should look like this inside your test:

这只是一个简单的例子.一个好的做法是检查传递给您的模拟的参数是否等于预期的参数.一种方法是用真实的预期数据替换 Mockito.eq().另一种是单独验证,像这样:

This is just a simple example. A good practice would be to check that the arguments passed to your mock equal to the expected ones. One way would be to replace Mockito.eq() with the real expected data. Another is to verify it separately, like this:

public ResponseEntity<String> postJson(Set<Type> Types){
            try {
                String oneString = String.join(",", Types);
               Map<String, String> requestBody = new HashMap<>();
                requestBody.put("type", oneString);
                JSONObject jsonObject = new JSONObject(requestBody);
                HttpEntity<String> request = new HttpEntity<String>(jsonObject.toString(), null);
    ResponseEntity result = restTemplate.exchange(url, HttpMethod.POST,
                        new HttpEntity<>(request, getHttpHeaders()), String.class);
                } 
        }
        return Types;

您可以按如下方式编写上述方法的测试

You can write test for above method as follows

   @Mock
   RestTemplate restTemplate;

   private Poster poster;
   
   HttpEntity<String> request = new HttpEntity<>(jsonObject.toString(), getHttpHeaders());
   
   ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST, request, String.class);
   
   Mockito.verify(restTemplate, Mockito.times(1)).exchange(
       Mockito.eq(uri),
       Mockito.eq(HttpMethod.POST),
       Mockito.eq(request),
       Mockito.eq(String.class));
   
   Assert.assertEquals(result, poster.postJson(mockData));
   
   HttpHeaders getHttpHeaders() {
       HttpHeaders headers = new HttpHeaders();
       headers.add(// whatever you need to add);
       return headers;
   }

这篇关于Mockito 单元测试 RestTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆