我有下一段代码:

public void init() {
    facultyList = new ArrayList<>();
    localeFile = new Properties();
    try {
        localeFile.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(loadFileName()));
    } catch (IOException | NullPointerException e) {
        LOG.error("Host: " + ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRemoteHost()
                + " | Error in constructing of LocaleModel. More: " + e);
    }
}


模拟Thread.currentThread().getContextClassLoader()的任何可能解决方案,或者如何测试此catch块?

最佳答案

您可以设置自定义上下文类加载器。跟踪原始上下文类加载器,并在完成测试后将其放回上下文,这一点很重要。

http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setContextClassLoader(java.lang.ClassLoader)

关于java - 在测试初始化​​方法中模拟Thread.currentThread()。getContextClassLoader(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24099056/

10-14 12:25
查看更多