简体   繁体   中英

How to test void method?

public void checkAlreadyUsedOrValidHealthDashBoardToken(ServiceCode serviceCode) {
    if(serviceCode.getHealthDashBoardToken().isEmpty()) {
        return;
    } else if(isAlreadyUsedHealthDashBoardToken(serviceCode)) {
        throw new GlobalException(ResponseType.SERVICE_ALREADY_USED_HEALTH_DASHBOARD_TOKEN);
    } else if(!isValidHealthDashBoardToken(serviceCode)) {
        throw new GlobalException(ResponseType.SERVICE_NOT_VALID_HEALTH_DASHBOARD_TOKEN);
}
}

@Test(expected = GlobalException.class)
public void checkAlreadyUsedOrValidHealthDashBoardTokenTest() {
    List<ServiceCode> serviceCodeList = new ArrayList<ServiceCode>();
    serviceCodeList.add(serviceCode);

    Mockito.when(serviceManageMapper.selectServiceByHashBoardToken((ServiceCode)notNull())).thenReturn(serviceCodeList);
    // execute test
}

I just want to test void method. how can I execute the test? Please help me.

In general if you want to test it should have a return type. But then if you want to test in this manner then,

@Test
public void checkAlreadyUsedOrValidHealthDashBoardTokenTest()
{
    <Enter prerequisite codes>
    int flag=0;
    try
    {
        checkAlreadyUsedOrValidHealthDashBoardToken(serviceCode);
    }
    catch(GlobalException ge)
    {
        if(GlobalException Response Type is SERVICE_ALREADY_USED_HEALTH_DASHBOARD_TOKEN)
        {
            falg=1;
        }
        else if(GlobalException Response Type is SERVICE_NOT_VALID_HEALTH_DASHBOARD_TOKEN)
        {
            flag=2;
        }
    }
    finally
    {
        assertEquals(flag,0/1/2);
    }

}

Enter three types of data and check 0,1 and 2 states. This is not at all good coding. But then it might be something you want. NB. Some codes are roughly written as logic above enter proper code on the basis of your class implementation.

In my opinion, I think that the function name is a bit too long, but whatever.

As for your question, what do you mean by test? Debug? If so, I would scatter print statements throughout the code to test it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM