不在类路径中的依赖项的测试资源?

不在类路径中的依赖项的测试资源?

问题描述:

我有一个使用Maven设置的多模块Spring项目:

I have a multi module Spring project that I set up using Maven:

my-root (pom)
    - my-logic
    - my-webapp (depending on my-logic)
    - my-consoleapp (depending on my-logic)

我的测试类继承自 AbstractTransactionalJUnit4SpringContextTests 并使用 @ContextCofiguration 用于设置 ApplicationContext

My Test classes inherit from AbstractTransactionalJUnit4SpringContextTests and use @ContextCofiguration for setting up the ApplicationContext.

例如Spring Controller的测试类:

E.g. the test class for a Spring Controller:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { 
  "classpath:applicationContext-logic-test.xml",
  "classpath:applicationContext-web-test.xml"})
public class ControllerTest extends AbstractTransactionalJUnit4SpringContextTests {

  @Autowired
    private ApplicationContext applicationContext;
    ...
}

如你所见,有一个配置XML每个模块。我有单独的配置进行测试,驻留在每个模块的测试/资源中(另外还有后缀-test)。这一切都有效(类编译,运行和JUnit测试成功)如果我在Eclipse中运行JUnit测试。

As you can see there is a config XML per module. I have seperate configs for tesing, residing in test/resources of each module (and additionaly having the suffix "-test"). This all works (the class compiles, runs and the JUnit tests are successful) if I run the JUnit test in Eclipse.

现在我的问题:使用Maven运行测试不起作用!(例如在 my-root 上使用运行方式>Maven安装(我使用) m2eclipse的))。具体来说,它将引发以下异常:

Now to my problem: Running the test using Maven will NOT work! (e.g. with "Run As">"Maven install" on my-root (I use m2eclipse)). Specifically, it will throw the following exception:


java.io.FileNotFoundException:类路径资源[applicationContext-logic-test.xml]不能因为它不存在而被打开`

java.io.FileNotFoundException: class path resource [applicationContext-logic-test.xml] cannot be opened because it does not exist`

似乎Maven没有添加来自 my-logic的文件/ src / test / resources 到运行 my-webapp 的单元测试时设置的类路径。

It seems that Maven does not add the files from my-logic/src/test/resources to the classpath that is set up when running the unit tests of my-webapp.

我该如何解决这个问题?

How can I fix that?


似乎Maven不会将 my-logic / src / test / resources 中的文件添加到运行my-webapp的单元测试时设置的类路径中。

It seems that Maven does not add the files from my-logic/src/test/resources to the classpath that is set up when running the unit tests of my-webapp.

不,确实没有。首先,Maven使用始终通过本地存储库解析的二进制依赖项。第二,二进制依赖项不包括测试内容。

No, indeed, it doesn't. First, Maven uses binary dependencies that are always resolved through the local repository. And second, binary dependencies don't include test stuff.

但你可以做的是:


  1. 配置 my-logic 模块,使用 jar:test-jar

  2. 配置 my-webapp 模块以依赖此测试JAR(使用 test 范围)。

  1. configure the my-logic module to create a test JAR using jar:test-jar
  2. configure the my-webapp module to depend on this test JAR (using a test scope).

对于#1,你必须在 my-logic的pom.xml中配置Maven Jar插件

<project>
  <build>
    <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-jar-plugin</artifactId>
       <version>2.2</version>
       <executions>
         <execution>
           <goals>
             <goal>test-jar</goal>
           </goals>
         </execution>
       </executions>
     </plugin>
    </plugins>
  </build>
</project>

Maven将创建一个内容为 target / test-classes的JAR 包期间并安装/部署它。

And Maven will create a JAR with the content of target/test-classes during package and install / deploy it.

对于#2,声明一个依赖项在 my-webapp 的pom.xml中的测试JAR上:

For #2, declare a dependency on the test JAR in the pom.xml of my-webapp:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>com.myco.app</groupId>
      <artifactId>foo</artifactId>
      <version>1.0-SNAPSHOT</version>
      <type>test-jar</type>
      <scope>test</scope>
    </dependency>
  </dependencies>
  ...
</project>

应该这样做。