我正在尝试模拟下面的rest调用
restTemplate.exchange(url, HttpMethod.PUT, new HttpEntity<User>(user), void.class);这是JUNIT
Mockito.when(restTemplate.exchange(Matchers.any(), Matchers.eq(HttpMethod.PUT), Matchers.any(HttpEntity.class), Matchers.eq(Void.class))).thenReturn(new ResponseEntity<>(HttpStatus.ACCEPTED));但问题出在我调试代码的时候。即使我从mock抛出一个异常,它也总是返回NULL。
我不确定我到底做错了什么
发布于 2018-03-23 21:08:45
这是因为Void和void.class类是不同的:
Class<?> c1 = void.class;
Class<?> c2 = Void.class;
System.out.println(c1.equals(c2));打印false。
尝试以下操作:
Mockito.when(restTemplate.exchange(Matchers.any(),
Matchers.eq(HttpMethod.PUT),
Matchers.any(HttpEntity.class),
Matchers.eq(void.class)))
.thenReturn(new ResponseEntity<>(HttpStatus.ACCEPTED));https://stackoverflow.com/questions/49446320
复制相似问题