[英]Add src/main/resources to classpath when running unit test in eclipse for spring
In eclipse my maven project has a 'src/main/resources/' folder and a 'src/test/resources' folder.在 Eclipse 中,我的 maven 项目有一个“src/main/resources/”文件夹和一个“src/test/resources”文件夹。 The contents of both directories seem to go into 'Project/target/classes', and the content of just the 'src/test/resources' directory goes into 'Project/target/test-classes'.
两个目录的内容似乎都进入“Project/target/classes”,而“src/test/resources”目录的内容进入“Project/target/test-classes”。
When I use spring to load a classpath resource whilst testing, it seems to only have available the stuff that's in 'test-classes' folder on the classpath.当我在测试时使用 spring 加载类路径资源时,它似乎只有类路径上“test-classes”文件夹中的内容可用。 This makes sense, as only testing resources go in the classpath for testing.
这是有道理的,因为只有测试资源进入类路径进行测试。
However I want my main resources to be available when running unit tests aswell, but they cannot be found when running unit tests as they are not on the classpath.但是,我希望我的主要资源在运行单元测试时也可用,但是在运行单元测试时无法找到它们,因为它们不在类路径中。 How do I configure my way around this problem?
我如何配置我的方式来解决这个问题?
You can configure the surefire plugin (which is what maven uses to run unit tests) to include any additional classes, jars or resource directories.您可以配置 surefire 插件(这是 maven 用来运行单元测试的)以包含任何其他类、jar 或资源目录。
http://maven.apache.org/plugins/maven-surefire-plugin/examples/configuring-classpath.html http://maven.apache.org/plugins/maven-surefire-plugin/examples/configuring-classpath.html
When using TestNG to test a project that contains MyBatis, an exception may not be found in the XML file使用TestNG测试一个包含MyBatis的项目时,可能在xml文件中找不到异常
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): org.apache.ibatis.binding.BindingException:无效的绑定语句(未找到):
Solution In the pom.xml file, point the Test's resources directory to src/main/resources解决方法在pom.xml文件中,将Test的resources目录指向src/main/resources
<build>
<testResources>
<!--测试的时候直接读取src/main/resources的资源文件-->
<testResource>
<directory>${project.basedir}/src/main/resources</directory>
</testResource>
</testResources>
</build>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.