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

发布于 2024-09-16 04:48:42 字数 1258 浏览 8 评论 0原文

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

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

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

例如,Spring 控制器的测试类:

@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 测试。

现在我的问题是:使用 Maven 运行测试将不起作用!(例如,在 my-root 上使用“Run As”>“Maven install”(我使用 m2eclipse) )。具体来说,它会抛出以下异常:

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

看来 Maven 没有添加 my-logic/src/test/resources< 中的文件/code> 到运行 my-webapp 的单元测试时设置的类路径。

我该如何解决这个问题?

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)

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

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;
    ...
}

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.

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: class path resource [applicationContext-logic-test.xml] cannot be opened because it does not exist`

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回梦 2024-09-23 04:48:42

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

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

但你可以做的是:

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

对于 #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 /code> 在 package 期间安装/部署它。

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

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

这样就可以了。

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.

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.

But what you could do is:

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

For #1, you'll have to configure the Maven Jar Plugin in the pom.xml of my-logic:

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

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

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>

That should do it.

吐个泡泡 2024-09-23 04:48:42

这是设计使然。测试资源不会放入输出工件中,因此您所依赖的任何测试依赖项都将无法访问,即使在单元测试类路径中也是如此。

如果您想完成您想要做的事情,您应该创建一个项目,其中包含测试所需的资源,并使其成为 my-logic 和 my-webapp 的测试范围依赖项

That's by design. Test resources are not placed into the output artifact, so any test dependencies of something you depend on would not be accessible, even in your unit test classpath.

If you want to accomplish what you're trying to do you should create a project which contains the resources you need for testing and make it a test scoped dependency of both my-logic and my-webapp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文