我的课:
public class Util {
private static final CustomLogger LOGGER = new CustomLogger(Util.class);
public static void commitToDB() {
try {
CommitToDB();
} catch (SQLException sqlException) {
LOGGER.error("Exception in DB Commit", sqlException.getMessage(), sqlException);
}
}
}
我的单元测试用例类:
@RunWith(PowerMockRunner.class)
@PrepareForTest({
CustomLogger.class
})
public class UtilTest {
@Mock
private Util util;
@Mock
private CustomLogger CustomLogger;
public void verifyFailedCommitRecords() throws SQLException {
SQLException sqlException = new SQLException("Exception in DB Commit");
doThrow(sqlException).when(protectedConn).commit();
CustomLogger logger = PowerMockito.mock(CustomLogger.class);
Util.CommitToDB();
verify(logger, times(1)).error("Exception in DB Commit", sqlException.getMessage(), sqlException);
}
}
我收到错误消息,指出“实际上,与此模拟的交互为零。” 通缉但未调用:
PS:Util.commitToDB 调用了一个内部类protectedConnection,它抛出了SQLexception。