简体   繁体   English

如何测试void方法?

[英]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.我只想测试 void 方法。 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.输入三种类型的数据并检查 0,1 和 2 状态。 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.如果是这样,我会在整个代码中散布打印语句来测试它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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