Spring 如何为Resttemplate交换方法编写mockito junit

Spring 如何为Resttemplate交换方法编写mockito junit,spring,junit,mockito,resttemplate,Spring,Junit,Mockito,Resttemplate,如何为下面的方法编写mockito junit: @Autowired RestTemplate restTemplate; ResponseEntity<?> execute(final String url, HttpMethod httpMethod, HttpEntity<?> entityRequest, String.class,

如何为下面的方法编写mockito junit:

@Autowired
RestTemplate restTemplate;

ResponseEntity<?> execute(final String url, HttpMethod httpMethod,
                          HttpEntity<?> entityRequest,
                          String.class, 
                          Map<String, String> urlVariables){
    restTemplate.exchange(url, httpMethod, entityRequest, responseType, urlVariables);
}
@Autowired
rest模板rest模板;
响应执行(最终字符串url、HttpMethod、HttpMethod、,
HttpEntity实体请求,
String.class,
映射(变量){
exchange(url、httpMethod、entityRequest、responseType、urlVariables);
}

请帮我写。这取决于你想要什么

使用mock的一种方法是使调用
execute
方法更容易。为此,可以使用实际参数的模拟版本,例如
HttpMethod
HttpEntity
。如果基础的
exchange
方法需要这些参数中的某些行为,那么您可能需要在
时使用mockito的
存根<编码>然后返回
方法

一旦设置了这些模拟参数,以便您可以调用
execute
方法,您将需要检查其结果是否正确

为了检查返回值,可以使用传统的JUnit断言

此外,您可能希望检查是否确实发生了与模拟对象的某些交互。为此,您可以使用mockito的
verify
方法来检查,例如,是否实际调用了某些
HttpEntity
方法

从技术上讲,您还可以验证是否调用了rest模板的
exchange
方法。为此,您需要模拟RestTemplate,并将模拟注入测试中的类中。然后可以使用mockito的
verfiy
检查
exchange
是否以正确的方式调用。这通常是明智的做法,尤其是当您的类中有更多的方法需要测试时。对于当前的
execute
方法,它似乎有点过分了。

@RunWith(MockitoJUnitRunner.class)
@RunWith(MockitoJUnitRunner.class)
public class ToTestTest {

  @InjectMocks
  private YourClass toTest;

  @Mock
  private RestTemplate template;

  @Test
  public void test() {
    toTest.execute(Mockito.anyString(), Mockito.any(), Mockito.any(), 
                   Mockito.any(), Mockito.any());

    Mockito.verify(template, Mockito.times(1))
                    .exchange(Mockito.anyString(),
                                    Mockito.<HttpMethod> any(),
                                    Mockito.<HttpEntity<?>> any(),
                                    Mockito.<Class<?>> any(),
                                    Mockito.<String, String> anyMap());
    }
 }
公共类测试{ @注射模拟 二等兵你的班级去测试; @嘲弄 私有rest模板; @试验 公开无效测试(){ toTest.execute(Mockito.anyString(),Mockito.any(),Mockito.any(), Mockito.any(),Mockito.any()); 验证(模板,Mockito.times(1)) .exchange(Mockito.anyString(), Mockito.any(), Mockito.>any(), Mockito.anyMap()); } }
我认为在这种情况下,最方便、最合适的方法(即使用
RestTemplate
的客户端REST测试)将由framework提供。

我曾经遇到过这样的错误。我找到了一个更可靠的解决方案。我也提到了对我有效的进口声明。下面的代码完全模拟了rest模板

import org.mockito.Matchers;  
import static org.mockito.Matchers.any;

    HttpHeaders headers = new Headers();
    headers.setExpires(10000L);     
    ResponseEntity<String> responseEntity = new ResponseEntity<>("dummyString", headers, HttpStatus.OK);
    when(restTemplate.exchange( Matchers.anyString(), 
            Matchers.any(HttpMethod.class),
            Matchers.<HttpEntity<?>> any(), 
            Matchers.<Class<String>> any())).thenReturn(responseEntity);
import org.mockito.Matchers;
导入静态org.mockito.Matchers.any;
HttpHeaders=新标题();
标题。设置过期(10000L);
ResponseEntity ResponseEntity=新的ResponseEntity(“dummyString”,标题,HttpStatus.OK);
当(restTemplate.exchange)(Matchers.anyString()),
Matchers.any(HttpMethod.class),

Matchers。在这种情况下,您必须模拟rest模板。然后只需模拟方法调用:
Mockito.when(restemplate.exunge(…)。然后返回(…)
。有什么问题吗?
execute
方法不应该返回
exchange
结果吗?是否要测试方法逻辑并模拟restTemplate结果?这很好地显示了mockito机制。详细信息:您需要括号
(…)
任何
调用之前,围绕类型强制转换。但是您假设在
YourClass
中不创建
新RestTemplate()
?Mockito为您创建resttemplate的模拟实例
参数匹配器的使用无效!需要4个匹配器,1个记录:
@ClaudioPomo当然您不需要
新建resttemplate()
-这将是一个硬依赖项,不能模拟,应该在代码中避免(即使您根本不编写测试)。