如何解决断言void方法junit测试
我正在尝试为下面提到的void方法编写测试用例。有人可以帮助我使用正确的assert语句来测试此方法。
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?*/
}
}
解决方法
如何验证某些函数被调用?
演示:
[Microsoft.AspNetCore.Mvc.HttpPostAttribute("events")]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。