How to use maven resources also as test resources

五迷三道 提交于 2019-12-18 04:38:15

问题


I have a maven project that loads an xslt file and executes the transformation along with other processing on the result. Normally when the user runs the application, the user provides the xslt file path to be loaded. But I include some default xslt files bundled inside my application that the user can use without loading any external xslt file. I do this by adding them to src/main/resources/xslt. My problem is that I want to run tests against those xslt files in testing phase. How can I achieve this? Should I copy the src/main/resources/xslt contents to target/somewhere and load these in my test classes code? Which plugin is used for that?


回答1:


My problem is that I want to run tests against those xslt files in testing phase. How can I achieve this?

There is nothing to do, target/classes is on the class path of tests. More precisely, the class path for tests is:

  • first target/test-classes
  • then target/classes
  • then dependencies

So resources from src/main/resources (which are copied into target/classes) are visible from tests.




回答2:


If you put a file foo.txt inside src/test/resources/, you can open this via:

// try-with-resource (Java 1.7)
try (InputStream is = getClass().getClassLoader().getResourceAsStream("foo.txt")) {
    // do something with is...
}

You can also take a look at the maven-resources-plugin.



来源:https://stackoverflow.com/questions/2978471/how-to-use-maven-resources-also-as-test-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!