【发布时间】:2017-02-27 23:12:23
【问题描述】:
在 Spock 单元测试中,一个方法会拉入资源 JSON 文件...在检索基于类路径的资源的方法中:
Myclass.classpath.getResource('/someJsonfile.json') //Works in WAR arhcive, but fails during a spock test and returns null
上述方法不起作用,但在部署 WAR 时该功能可以正常工作
以下在 Spock 中用于解析类路径资源,但是会破坏 WAR 的功能
System.getResource('/someJsonfile.json') //Works in spock test, but breaks functionality when inside WAR
【问题讨论】:
-
你试过了吗:
this.getClass().getResource('/someJsonfile.json')或者,如果 Myclass 是 groovy,Myclass.getResource('/someJsonfile.json'),或者如果它是 javaMyclass.class.getResource('/someJsonfile.json') -
谢谢,是的,上面的代码是用 Groovy 运行的 :) ...问题是一旦打包到 WAR 中就可以工作了...我可以检查这些细微的差异是否会产生影响,并且回来报告。再次感谢!
标签: java groovy classpath spock