mogronalol
mogronalol

Reputation: 3015

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. 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'.

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. 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?

Upvotes: 0

Views: 10928

Answers (2)

Huang Jinlong
Huang Jinlong

Reputation: 1024

When using TestNG to test a project that contains MyBatis, an exception may not be found in the XML file

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

  1. Assume that the XML files corresponding to your MyBatis are all placed under src/main/resources/mapper
  2. After compilation, the target/classes folder will contain the mapper folder
  3. There is no mapper folder in the target/test-classes folder! So the error is reported

Solution In the pom.xml file, point the Test's resources directory to src/main/resources

    <build>
        <testResources>
            <!--测试的时候直接读取src/main/resources的资源文件-->
            <testResource>
              <directory>${project.basedir}/src/main/resources</directory>
            </testResource>
        </testResources>
    </build>

Chinese description 中文说明

Upvotes: -1

Spencer Kormos
Spencer Kormos

Reputation: 8441

You can configure the surefire plugin (which is what maven uses to run unit tests) to include any additional classes, jars or resource directories.

http://maven.apache.org/plugins/maven-surefire-plugin/examples/configuring-classpath.html

Upvotes: 2

Related Questions