在编写logger.isDebugEnabled()的测试时需要帮助

时间:2016-06-24 05:12:59

标签: junit

以下是更新完整代码:

public Info verify(final String accessTk) throws AuthenticationException {

    final Info accessInfo = readAccessToken(accessTk);

    if (logger.isDebugEnabled()) {

        logger.debug(accessInfo);
    }


    verifyTkExpire(accessInfo);

    return accessInfo;

}

我不知道使用记录器,因此试图找到一种方法来测试它。

1 个答案:

答案 0 :(得分:0)

为了清楚起见,我建议发布整个方法/功能(每个方法/功能只能做一个左右的事情,以提高单元测试的测试能力);基于你给出的东西,可能会做类似的事情:

/**
* Tests tryDebug
*/
@Test
public void tryDebugTest() {
    logger testLogger= new logger() //or however you would initialize logger
    Info testInfo= new Info(someInfo); //same type as accessInfo
    Info expected= new Info(expectedInfo);  //expected result from debugging the test info
    logger.enableDebug;
    logger.debug(testInfo); //debugs testInfo
    assertEquals(expected, testInfo);
    //if 'Info' is an array use assertArrayEquals(expected, testInfo);
    //if 'Info' is a class, use something like this for each of it's elements: 
    //assertEquals(expected.getVariable, testInfo.getVariable); 
}