Evgeny Mironenko
Evgeny Mironenko

Reputation: 439

Mock Thread.currentThread().getContextClassLoader() in test init method

I have the next block of code:

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);
    }
}

Any possible solutions to mock Thread.currentThread().getContextClassLoader() or how can I test this catch block?

Upvotes: 3

Views: 2683

Answers (1)

Brett Okken
Brett Okken

Reputation: 6306

You can set a custom context class loader. It is important to keep track of the original context class loader and put it back into context when your test is done.

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

Upvotes: 4

Related Questions