Junit + getResourceAsStream Returning Null

后端 未结 13 1427
情话喂你
情话喂你 2020-12-15 03:32

Not sure how this is possible. I re-read up on getResourceAsStream and it\'s always returning null.

InputStream source = this.getClass().getResourceAsStream(         


        
13条回答
  •  余生分开走
    2020-12-15 03:46

    I had this problem with a Spring class. System.class.getResourceAsStream(...) worked in unit tests but not in Tomcat, Thread.currentThread().getContextClassLoader().getResourceAsStream(...) worked in Tomcat but not in unit tests.

    Ulitimately I went with:

    ClassUtils.getDefaultClassLoader()
        .getResourceAsStream("com/example/mail/templates/invoice-past-due.html"))
    

    I also found that once I did it this way, I did not need to have the path starting with a slash.

提交回复
热议问题