春天 – 如何编写mockito junit为Resttemplate交换方法

前端之家收集整理的这篇文章主要介绍了春天 – 如何编写mockito junit为Resttemplate交换方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_1@如何编写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);
}

请帮我写如何写

解决方法

@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.verify(template,Mockito.times(1))
                    .exchange(Mockito.anyString(),Mockito.<HttpMethod> any(),Mockito.<HttpEntity<?>> any(),Mockito.<Class<?>> any(),Mockito.<String,String> anyMap());
    }
 }

猜你在找的Java相关文章