如何测试void方法

时间:2019-02-05 11:02:00

标签: mockito junit5

如何测试void方法?

@Override
    public void insertUserDetails(String userData) throws JSONException {
        Map<String, Object> userDataMap = jsonUtil.convertJsonToMap(userData);
        dao.insertUserDetails(userDataMap);
    }

我尝试了很多方法,但是没有解决。

2 个答案:

答案 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());
    }