Java 如何配置用于 junit 测试的 eclipse 类路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1311889/
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
How do I configure eclipse classpath used for junit tests?
提问by wds
I have an eclipse project where every source folder has its own associated output folder. Instead of /classes it's called /eclipse-classes.
我有一个 eclipse 项目,其中每个源文件夹都有自己关联的输出文件夹。它被称为 /eclipse-classes,而不是 /classes。
So if I have a folder: src/main/java (typical maven thing) the target folder is: target/eclipse-classes
所以如果我有一个文件夹:src/main/java(典型的 maven 东西)目标文件夹是:target/eclipse-classes
And likewise for resources etc.
资源等也是如此。
This seems to work (i.e. eclipse generates .class files that are put inside these folders) but running any Junit tests throws an exception stating "class not found". I'm running JUnit using the built-in eclipse test runner (i.e. right click the class, "run as", "Junit test").
这似乎有效(即 eclipse 生成放在这些文件夹中的 .class 文件),但运行任何 Junit 测试都会引发异常,指出“找不到类”。我正在使用内置的 eclipse 测试运行器运行 JUnit(即右键单击该类,“运行方式”,“Junit 测试”)。
Copying the /eclipse-classes folder to /classes makes them succeed, meaning eclipse is using /classes, but I can't find any configuration options to change it. Is there any way to find out where and why eclipse is still using the /classes folder?
将 /eclipse-classes 文件夹复制到 /classes 使它们成功,这意味着 eclipse 正在使用 /classes,但我找不到任何配置选项来更改它。有什么方法可以找出 eclipse 仍在使用 /classes 文件夹的位置和原因?
(perhaps relevant, I'm also using the m2eclipse plugin)
(也许相关,我也在使用 m2eclipse 插件)
Some additional information inspired by Rich Seller's answer: Maven is configured to run the following on resource changes:
受 Rich Seller 的回答启发的一些附加信息:Maven 被配置为在资源更改时运行以下内容:
process-resources resources:testResources
While this won't do anything useful (copies to the wrong directory) the resources are not problematic atm since they end up in the correct location.
虽然这不会做任何有用的事情(复制到错误的目录),但资源在 atm 中没有问题,因为它们最终位于正确的位置。
The .classpath entries look okay. There's a bunch of maven subprojects so the nesting goes a bit deeper than what Rich posted, but otherwise it's exactly the same except for this line:
.classpath 条目看起来不错。有一堆 maven 子项目,所以嵌套比 Rich 发布的要深一些,但除此行外,其他方面完全相同:
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
I think we might not need that one but it's not hurting anything atm.
我认为我们可能不需要那个,但它不会伤害任何自动取款机。
edit2: Further testing reveals that eclipse is generating class files in both the /eclipse-classes folders and the /classes folder. It seems that m2eclipse is running mvn build in the background when building automatically, but I can't seem to find a way to disable this. I'll try to get in touch with the m2eclipse developers if nobody here has any other ideas.
edit2:进一步的测试表明 eclipse 正在 /eclipse-classes 文件夹和 /classes 文件夹中生成类文件。似乎m2eclipse在自动构建时在后台运行mvn build,但我似乎找不到禁用它的方法。如果这里没有人有任何其他想法,我将尝试与 m2eclipse 开发人员取得联系。
回答by Aaron Digulla
If you use m2eclipse, then the config in the Eclipse project is overwritten by the plugin. See this articlefor a solution.
如果您使用 m2eclipse,那么 Eclipse 项目中的配置将被插件覆盖。请参阅本文以获取解决方案。
The reason for this is that some maven plugins can't cope with a directory that is outside of target/
, so the m2eclipse devs force the folders for compiled classes to be target/classes
and target/test-classes
, no matter what you configure in Eclipse.
这样做的原因是一些 Maven 插件无法处理 . 之外的目录target/
,因此 m2eclipse 开发人员强制编译类的文件夹为target/classes
and target/test-classes
,无论您在 Eclipse 中配置什么。
By using a profile, you can use different folders for Eclipse. Still, it's not wise to change the output folders for maven (and its plugins).
通过使用配置文件,您可以为 Eclipse 使用不同的文件夹。尽管如此,更改 maven(及其插件)的输出文件夹并不明智。
回答by Rich Seller
The Eclipse JUnit integration has no special classpath configuration, it will work off the output folders defined in your classpath and should find all classes compiled to those folders. It may be that there is something dodgy in your .classpath file, so that JUnit is confused (by default the .classpath file is hidden from view, it is located in the root of the project).
Eclipse JUnit 集成没有特殊的类路径配置,它将处理类路径中定义的输出文件夹,并且应该找到编译到这些文件夹的所有类。可能是您的 .classpath 文件中存在一些不可靠的内容,从而使 JUnit 感到困惑(默认情况下,.classpath 文件是隐藏的,它位于项目的根目录中)。
Based on your description I would expect to see entries something like below (note the default output folder and the override for src/main/java and src/main/resources). Does your classpath look markedly different, if so that may be the issue
根据您的描述,我希望看到类似下面的条目(注意默认输出文件夹以及 src/main/java 和 src/main/resources 的覆盖)。您的类路径是否看起来明显不同,如果是,那可能是问题所在
<classpathentry excluding="**" kind="src" output="target/eclipse-classes" path="src/main/resources"/>
<classpathentry kind="src" output="target/eclipse-classes" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
This is a long shot, but it may also be that a Maven clean is configured on your project, if that is the case the contents of target/eclipse-classes would be dropped whenever the clean goal is run, so your tests would be deleted from the file system before the tests are run. You can see what goals are run by Maven by opening the project properties (alt-enter) and selecting the Maven item.
这是一个长镜头,但也可能是在您的项目中配置了 Maven clean,如果是这种情况,则每当运行 clean 目标时,target/eclipse-classes 的内容都会被删除,因此您的测试将被删除在运行测试之前从文件系统。您可以通过打开项目属性 ( alt-enter) 并选择 Maven 项来查看 Maven 运行的目标。
This part doesn't directly answer your question, but you may find useful anyway. I tend to have my Eclipse output directories the same as for Maven and have no issues inside Eclipse (I modify the Maven builder to only run process-resources so it doesn't attempt to compile).
这部分不会直接回答您的问题,但无论如何您可能会觉得有用。我倾向于让我的 Eclipse 输出目录与 Maven 相同,并且在 Eclipse 内部没有问题(我修改了 Maven 构建器以仅运行进程资源,因此它不会尝试编译)。
If I do a Maven build the Maven compiler will build any changed classes (that would be all of them if a clean is included). A subsequent modification in Eclipse is detected by the incremental compiler and processed, all is fine. I do turn off Build Automatically, but that's just because it annoys me, it may be that Maven and Eclipse fght if you have them both turned off.
如果我进行 Maven 构建,则 Maven 编译器将构建任何更改的类(如果包含 clean 则将是所有这些类)。Eclipse 中的后续修改被增量编译器检测并处理,一切正常。我确实关闭了自动构建,但这只是因为它让我烦恼,如果您将它们都关闭,则可能是 Maven 和 Eclipse fght。
回答by dirtyvagabond
Just in case you're open to trying a different plugin for this: I use the maven-eclipse-plugin to generate my Eclipse project settings. I configure the plugin to configure my Eclipse project to use a completely separate output directory for classes (see below). It's relative to the project root, so it sits outside of target.
以防万一您愿意为此尝试不同的插件:我使用 maven-eclipse-plugin 来生成我的 Eclipse 项目设置。我配置插件来配置我的 Eclipse 项目以使用完全独立的类输出目录(见下文)。它相对于项目根目录,因此位于目标之外。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.6</version>
<configuration>
<outputDirectory>eclipse_build</outputDirectory>
</configuration>
</plugin>
This works great for me, including being able to run tests right out of the box, both via Maven and Eclipse.
这对我来说非常有用,包括能够通过 Maven 和 Eclipse 开箱即用地运行测试。
回答by Chacko Mathew
try running mvn eclipse:eclipse
. This fixed the issue for me
尝试运行mvn eclipse:eclipse
。这为我解决了这个问题
回答by RAY
I am using gradle. Refreshing gradle project will overwrite .classpath file. So I have to add the following at the end of the build.gradle file in order to let it maintain a JUNIT5 reference.
我正在使用gradle。刷新 gradle 项目将覆盖 .classpath 文件。所以我必须在 build.gradle 文件的末尾添加以下内容,以便让它保持 JUNIT5 引用。
eclipse.classpath.file.whenMerged {
classpath ->
def JUnitFive = new org.gradle.plugins.ide.eclipse.model.Container('org.eclipse.jdt.junit.JUNIT_CONTAINER/5')
JUnitFive.exported = true
classpath.entries << JUnitFive
}