以下是测试班
@RunWith(MockitoJUnitRunner.class)
public class AuditServiceClientTest {
private MockMvc mockMvc;
@Mock
private RestTemplate restTemplate;
@Mock
AuditServiceClient auditServiceClient;
@Mock
ICommonDataService iCommonDataService;
private AuditServiceResponse auditServiceResponse;
private AuditServiceLog auditServiceLog;
private HttpEntity<AuditServiceLog> request;
@Before
public void setUp() throws Exception {
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("X-AGS-Client-Name", "test");
headers.add("X-AGS-Group-Name", "test");
headers.add("Content-Type", "application/json");
auditServiceClient = new AuditServiceClientImpl();
iCommonDataService = new CommonDataService();
auditServiceLog = new AuditServiceLog();
request = new HttpEntity<AuditServiceLog>(auditServiceLog, headers);
auditServiceResponse = new AuditServiceResponse();
auditServiceResponse.setStatus(String.valueOf(200));
auditServiceResponse.setTimestamp("1990-01-01 00:00:01");
auditServiceResponse.setDescription("Description");
Mockito.when(restTemplate.postForObject(Mockito.anyString(), any(HttpEntity.class), ArgumentMatchers.eq(AuditServiceResponse.class)))
.thenReturn(auditServiceResponse);
String a = "test";
ArrayList<Integer> mockedList = new ArrayList<Integer>();
Mockito.when(iCommonDataService.getEnvValue(Mockito.anyInt(), Mockito.anyInt(), Mockito.anyString()))
.thenReturn(a);
}
@Test
public void postTest() {
AuditServiceResponse response = null;
try {
response = auditServiceClient.post("endpoint", auditServiceLog, true);
} catch (Exception e) {
e.printStackTrace();
}
Assert.assertTrue(Integer.parseInt(response.getStatus() )== 200);
}
}
Run Code Online (Sandbox Code Playgroud)
我正进入(状态
InvalidUseOfMatchersException:在此处检测到错误或错误使用的参数匹配器
在以下行的setUp()方法中:
Mockito.when(iCommonDataService.getEnvValue(Mockito.anyInt(), Mockito.anyInt(), Mockito.anyString()))
.thenReturn(a);
Run Code Online (Sandbox Code Playgroud)
以下是错误:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:在此处检测到放错位置或滥用的参数匹配器:
-> com.auditService.test.AuditServiceClientTest.setUp(AuditServiceClientTest.java:72)
您不能在验证或存根之外使用参数匹配器。
正确使用参数匹配器的示例:when(mock.get(anyInt()))。thenReturn(null);
doThrow(new RuntimeException())。when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains(“ foo”))如果最后一个匹配器正在返回诸如any()的对象,但存根方法签名期望使用原始参数,则此消息可能会在NullPointerException之后出现,在这种情况下,请使用原始替代方法。
when(mock.get(any())); //错误的使用,
当(mock.get(anyInt()));时会引发NPE //正确使用用法另外,可能会出现此错误,因为您将参数匹配器与无法模拟的方法一起使用。以下方法不能存根/验证:final / private / equals()/ hashCode()。不支持在非公共父类上声明的模拟方法。
关于此错误有很多文章,但没有一篇对我有用。是否有任何问题,Mockito.anyInt()因为Mockito.anyString()在上一行中使用了它,所以效果很好。任何帮助表示赞赏。
仔细查看您的测试代码:
iCommonDataService = new CommonDataService();
...
Mockito.when(iCommonDataService.getEnvValue(Mockito.anyInt(), Mockito.anyInt(), Mockito.anyString()))
.thenReturn(a);
Run Code Online (Sandbox Code Playgroud)
您在嘲笑对象的方法而不是嘲笑,这是导致异常的原因。
由于您已经有一个声明为wih @Mock注释的此类的模拟,因此只需删除此行即可iCommonDataService = new CommonDataService();。另一种方法是使用手动提供模拟Mockito.mock(CommonDataService.class)。
但是,如果要模拟原始对象的方法,则应Mockito.spy()改用。