蝴蝶不菲
不确定您的问题可能是什么,但此时我不妨发布一个工作示例。如前所述,您需要正确指定每个参数的类型,以便 mockito 可以定位到匹配的方法签名。有关处理旧 mockito 版本使用的可变参数的语法,请检查this answer.import static org.mockito.ArgumentMatchers.any;...@RunWith(MockitoJUnitRunner.class)public class MockitoTest { @Test public void test() throws Exception { RestTemplate api = Mockito.mock(RestTemplate.class); Object obj1 = new Object(); Object obj2 = new Object(); Object obj3 = new Object(); Mockito.when(api.getForObject(any(String.class),any(Class.class), ArgumentMatchers.<Object>any())).thenReturn(obj1); Mockito.when(api.getForObject(any(String.class),any(Class.class), any(Map.class))).thenReturn(obj2); Mockito.when(api.getForObject(any(URI.class),any(Class.class))).thenReturn(obj3); Assert.assertEquals(obj1, api.getForObject("", String.class)); Assert.assertEquals(obj1, api.getForObject("", String.class, obj1)); Assert.assertEquals(obj1, api.getForObject("", String.class, obj1, obj2)); Assert.assertEquals(obj1, api.getForObject("", String.class, obj1, obj2, obj3)); Assert.assertEquals(obj1, api.getForObject("", String.class, new Object[] {obj1,obj2,obj3})); Assert.assertEquals(obj2, api.getForObject("", String.class, new HashMap())); Assert.assertEquals(obj3, api.getForObject(new URI(""), String.class)); }}对于您的用例,只需将thenReturnwith替换为thenThrow.