简体   繁体   English

在 Eclipse 中为 spring 运行单元测试时将 src/main/resources 添加到类路径

[英]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:无效的绑定语句(未找到):

  1. Assume that the XML files corresponding to your MyBatis are all placed under src/main/resources/mapper假设你的MyBatis对应的xml文件都放在src/main/resources/mapper下
  2. After compilation, the target/classes folder will contain the mapper folder编译后,target/classes 文件夹将包含映射器文件夹
  3. There is no mapper folder in the target/test-classes folder! target/test-classes 文件夹中没有 mapper 文件夹! So the error is reported所以报错

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>

Chinese description 中文说明中文说明 中文说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用程序(src / main)运行时,类路径中的src / test / resources中的文件是? - Are files in src/test/resources on classpath while application (src/main) is running? 运行测试时,从类路径中排除src / main / resources - Exclude src/main/resources from classpath when running tests 让 Eclipse 使用 src/test/resources 而不是 src/main/resources - Make Eclipse use src/test/resources instead of src/main/resources src / main / resources中的文件不在classpath中 - file in src/main/resources is not in classpath 使用mvn exec:java时,如何将src / main / resources添加到类路径中 - How can I add src/main/resources to the classpath when using mvn exec:java Spring xml + JUnit-xml上下文文件在src / test / resources中不起作用,但是在src / main / resources中起作用 - Spring xml + JUnit - xml context file not working in src/test/resources but works in src/main/resources 测试时将使用src / main / resources和src / test / resources目录中的哪个application.yml - Which application.yml that're in src/main/resources and src/test/resources directories will be used when testing Eclipse 中 Maven 项目的类路径 - src vs src/main/java - Classpath for Maven project in Eclipse - src vs src/main/java 使用src / main和src / test运行测试时出现问题 - Problem running a test with src/main and src/test Maven项目不会将文件放在classpath的src / main / resources中 - Maven project doesn't put files in src/main/resources on classpath
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM