如何测试void方法?
@Override
public void insertUserDetails(String userData) throws JSONException {
Map<String, Object> userDataMap = jsonUtil.convertJsonToMap(userData);
dao.insertUserDetails(userDataMap);
}
我尝试了很多方法,但是没有解决。
答案 0 :(得分:1)
您可以使用Mockito覆盖行。
示例:
@Mock
private Dao dao;
@Test
public void insertUserDetails() {
when(jsonUtil.convertJsonToMap(any(UserData.class)).thenReturn(...);
obj.insertUserDetails(...);
verify(dao, times(1)).insertUserDetails(expected);
}
答案 1 :(得分:-1)
@Test
public void insertUserDetailsTest() throws JSONException{
String userdata = "{\"name\":\"test1\",\"dept\":\"test1\",\"id\":1}";
Mockito.doNothing().when(mdmdao).insertUserDetails(Mockito.anyMap());
mdmserviceimpl.insertUserDetails(userdata);
Mockito.verify(mdmdao,times(1)).insertUserDetails(Mockito.anyMap());
}