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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3571149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:27:32  来源:igfitidea点击:

Test-resources of dependencies not in classpath?

javaspringmaven-2junitclasspath

提问by apropoz

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

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

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

My Test classes inherit from AbstractTransactionalJUnit4SpringContextTestsand use @ContextCofigurationfor setting up the ApplicationContext.

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

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

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

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) ifI run the JUnit test in Eclipse.

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

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:

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

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

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

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

似乎 Maven 没有将文件添加my-logic/src/test/resources到运行my-webapp.

How can I fix that?

我该如何解决?

采纳答案by Pascal Thivent

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

Maven 似乎没有将文件添加my-logic/src/test/resources到运行 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.

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

But what you could do is:

但是你可以做的是:

  1. configure the my-logicmodule to create a test JAR using jar:test-jar
  2. configure the my-webappmodule to depend on this test JAR (using a testscope).
  1. 配置my-logic模块以使用创建测试 JARjar:test-jar
  2. 配置my-webapp模块以依赖此测试 JAR(使用test范围)。

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

对于 #1,您必须在 pom.xml 中配置 Maven Jar 插件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-classesduring packageand install / deploy it.

Maven 将创建一个包含target/test-classesduring内容的 JARpackage并安装/部署它。

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

对于 #2,在 pom.xml 中声明对测试 JAR 的依赖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.

那应该这样做。

回答by Jherico

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

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