使用过滤的资源调试 Maven junit 测试?
我们在 JUnit 测试中使用过滤后的 testResources,这些测试资源通常由 Maven Surefire 插件执行。也就是说,pom 包含一个部分
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
...
How can I run such JUnit-tests in the debugger?如果我在 eclipse 中执行测试,测试会失败,因为测试资源没有被过滤。如果过滤后的测试资源将写入目标目录中的某个位置,我可以将其用作附加源路径 - 但事实并非如此。如果我尝试使用 Debug As / maven test 在 eclipse 中运行 maven 构建,则构建不会在断点处停止。还有其他想法吗?
We are using filtered testResources in JUnit-tests that are usually executed by the maven surefire plugin. That is, the pom contains a section
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
...
How can I run such JUnit-tests in the debugger? If I execute the tests in eclipse the tests fail since the test resources are not filtered. If the filtered test resources would be written somewhere into the target directory, I could just use this as an additional source path - but this is not the case. If I try to run the maven build in eclipse with Debug As / maven test , the build does not stop in the breakpoints. Any other ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几种选择。首先,您可以从指定 maven.surefire.debug 的命令行运行测试。默认情况下,surefire 测试在分叉 JVM 中运行,这意味着如果您只是调试 Maven 进程,您将不会在测试断点处停止。这可能就是你现在看到的。另请参见 http://maven.apache.org/plugins/ maven-surefire-plugin/examples/debugging.html
否则,我建议在 IDE 中将您的项目配置为 Maven 项目。如果项目配置为 Maven 项目,则资源过滤将在运行测试之前自动进行。至少这就是它在 Idea 中的工作方式,我认为 Eclipse 在安装了正确的 Maven 插件后也会做同样的事情。
您还可以从命令行运行一次 Maven 构建,然后手动将 target/test-classes 目录添加到 IDE 配置中。有效,但有点狡猾。
There are several options. First you can run the test from the command line specifying maven.surefire.debug. Per default surefire tests are run in a forked JVM which means that if you just debug the maven process you won't get any stops in the test breakpoints. That's probably what you are seeing now. See also http://maven.apache.org/plugins/maven-surefire-plugin/examples/debugging.html
Othwerwise I would recommend to configure your project within the IDE as a maven project. If the project is configured as maven project the resource filtering will occur automatically prior to running the tests. That's at least how it works within Idea and I think Eclipse does the same with the right maven plugin installed.
You can also run the maven build once from the command line and then manually add the target/test-classes directory to your IDE configuration. Works, but is a little bit dodgy.
使用 m2eclipse 资源将在 Eclipse 内进行过滤。
Use m2eclipse and resources will get filtered inside Eclipse.