java - 断言无效方法junit测试
问题描述
我正在尝试为下面提到的 void 方法编写测试用例。有人可以帮助我使用正确的断言语句来测试此方法。
public void contextInitialized(com.servlet22.ServletContextEvent sce)
{
try {
// if 'corePoolSize' is 0 then it means no idle thread will be there. Only one active job will be functional.
int corePoolSize = Integer.parseInt(sce.getServletContext().getInitParameter(Constants.WOJOB_JOB_THREAD_NUMBER));
scheduler = new ScheduledThreadPoolExecutor(corePoolSize);
//context created
String woPropFile = EnvironmentBasedConfigurationHandler.getInstance().getValue(sce.getServletContext().getInitParameter(Constants.WO_CONFIGURATION_FILE));
Properties woProp = new Properties();
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(woPropFile);
woProp.load(is);
String ftpConfigFile = EnvironmentBasedConfigurationHandler.getInstance().getValue(sce.getServletContext().getInitParameter(Constants.FTP_CONFIGURATION_FILE));
String dbConfigFile = EnvironmentBasedConfigurationHandler.getInstance().getValue(sce.getServletContext().getInitParameter(Constants.DB_CONFIGURATION_FILE));
WOJob.init(ftpConfigFile, dbConfigFile, woProp);
WOJob woJob = WOJob.getInstance(scheduler);
scheduler.schedule(woJob, 2, TimeUnit.SECONDS);
logger.log("WOJobInitializerListener.contextInitialized()", "Configuration file path fetched from web.xml and WOJob has initialized and started");
} catch (Throwable th) {
StringWriter sw = new StringWriter();
th.printStackTrace(new PrintWriter(sw));
logger.log("WOJob.run()", "Unable to initialize WOJob Thread due to : " + sw.toString());
}
}
我写在下面的 Junit 测试 -
public class WOJobInitializerListenertest {
ServletContext servletCtx = Mockito.mock(ServletContext.class);
ServletContextEvent sce = Mockito.mock(ServletContextEvent.class);
@Test
public void test() {
Mockito.when(Integer.parseInt(sce.getServletContext().getInitParameter(Constants.WOJOB_JOB_THREAD_NUMBER))).thenReturn(0);
Mockito.when(EnvironmentBasedConfigurationHandler.getInstance().getValue(sce.getServletContext().getInitParameter(Constants.WO_CONFIGURATION_FILE))).thenReturn("wo.properties");
Mockito.when(EnvironmentBasedConfigurationHandler.getInstance().getValue(sce.getServletContext().getInitParameter(Constants.FTP_CONFIGURATION_FILE))).thenReturn("ftp.config");
Mockito.when(EnvironmentBasedConfigurationHandler.getInstance().getValue(sce.getServletContext().getInitParameter(Constants.DB_CONFIGURATION_FILE))).thenReturn("db.config");
WOJobInitializerListener el = new WOJobInitializerListener();
/*what assert can be used?*/
}
}
解决方案
如何验证是否调用了某些函数?
演示:
@Mock
ScheduledThreadPoolExecutor scheduler;
/*what assert can be used?*/
verify(scheduler).schedule(anyObject(), 2, TimeUnit.SECONDS);
推荐阅读
- r - 基于多个标准添加因子的 for 和 if 循环仅以最后一个簇结束
- r - 使用 openxlsx R 将不同工作簿中的工作表附加到一个工作簿中(保持格式)
- python - 无法在 MacOS 上使用 cx_Oracle,即使它已经安装
- python - 如何计算半重复行的出现次数并使计数成为新列
- python - 将文本文件编号存储为 Python 变量
- c++ - 在字符串上使用 cstring 有哪些好的场景?
- reactjs - 使用 redux 对带有单选按钮的 Rerender 进行反应
- python - Python Pandas 部分匹配数据框中的字符串列表
- c++ - 在 std::wstring 中转换 std::string 的模板函数,反之亦然,并带有编译时检查
- r - 如何更改数据的组织方式?