Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 932
Evgeny Mironenko Avatar asked Feb 28 '26 20:02

Evgeny Mironenko


1 Answers

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)

like image 174
Brett Okken Avatar answered Mar 02 '26 11:03

Brett Okken



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!