Eclipse/Maven:运行时未编译 JUnit 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6087690/
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-09-10 20:11:53  来源:igfitidea点击:

Eclipse/Maven: JUnit tests not compiled when running them

eclipsemavenjunit

提问by Tom

I am working on a project using Maven and Eclipse (m2eclipse plugin). I've got problems with the JUnit tests:

我正在使用 Maven 和 Eclipse(m2eclipse 插件)开发一个项目。我在 JUnit 测试中遇到了问题:

Sometimes, when running them within Eclipse, they wont be compiled, but the old class files are used instead. When I delete the class files, I get ClassNotFoundExceptionsin Eclipse. I then have to manually recompile them by using mvn test-compileor other goals.

有时,在 Eclipse 中运行它们时,它们不会被编译,而是使用旧的类文件。当我删除类文件时,我进入ClassNotFoundExceptions了 Eclipse。然后我必须通过 usingmvn test-compile或其他目标手动重新编译它们。

I also noticed that the class files of the tests sometimes are put into the classessubdirectory instead of test-classes.

我还注意到有时将测试的类文件放入classes子目录而不是test-classes.

I really can't figure out what is wrong.

我真的想不通出了什么问题。

The JUnit java files are within src/main/javaand are correctly named (*Test.java).

JUnit java 文件位于其中src/main/java并正确命名 ( *Test.java)。

Do I have to compile and run them always via Maven? Why doesn't Eclipse compile the files when I want to run them? (Interestingly, sometimesit does. Sometimes everything works perfectly.)

我是否必须始终通过 Maven 编译和运行它们?当我想运行文件时,为什么 Eclipse 不编译它们?(有趣的是,有时确实如此。有时一切正常。)

回答by apa64

I had the same problem with STS Eclipse (Spring development variant), m2e and JUnit. The solution was to set the output folder for src/test/javato target/test-classes:

我在使用 STS Eclipse(Spring 开发变体)、m2e 和 JUnit 时遇到了同样的问题。解决的办法是设置输出文件夹src/test/javatarget/test-classes

  1. Right click the src/test/javafolder in the Package Explorer
  2. Select Build Path-> Configure Output Folder
  3. Enter target/test-classes, click OK
  1. 右键单击src/test/java包资源管理器中的文件夹
  2. 选择构建路径->配置输出文件夹
  3. 回车target/test-classes,点击确定

Now the changes in test classes are compiled correctly and you should be able to run JUnit tests in Eclipse.

现在测试类中的更改已正确编译,您应该能够在 Eclipse 中运行 JUnit 测试。

The problem is that Eclipse compiles the unit tests to the default output folder target/classeswhile JUnit plugin correctly tries to run them from test-classes.

问题是 Eclipse 将单元测试编译到默认输出文件夹,target/classes而 JUnit 插件正确地尝试从test-classes.

There are a few duplicates to this question:

这个问题有几个重复:

回答by Roosh

In addition to the answer below

除了下面的答案

  1. Right click the src/test/java folder
  2. Select Build Path -> Configure Output Folder
  3. Enter target/test-classes, click OK
  1. 右键单击 src/test/java 文件夹
  2. 选择构建路径 -> 配置输出文件夹
  3. 输入目标/测试类,点击确定

you should check to ensure that your builder is setup correctly by right clicking your project and going to Properties -> Builder. If you see that your builder is missing, you need to install one. In my case, the maven project had an AspectJ dependency and when I used the Maven Eclipse plugin to build my Eclipse project, it was looking for an AspectJ builder by default. I installed the AspectJ development tools and it solved the problem.

您应该通过右键单击您的项目并转到Properties -> Builder来检查以确保您的构建器设置正确。如果您发现缺少构建器,则需要安装一个。就我而言,maven 项目有一个 AspectJ 依赖项,当我使用 Maven Eclipse 插件构建我的 Eclipse 项目时,它默认寻找 AspectJ 构建器。我安装了 AspectJ 开发工具,它解决了这个问题。

Hope this helps!

希望这可以帮助!

回答by oberlies

The most likely explanations for the problem you are facing is that the output folder of src/test/javais not correctly configured.

您面临的问题最可能的解释src/test/java是未正确配置的输出文件夹。

Instead of fixing this configuration manually, you can have m2eclipse fix this for you: Just right-click on the project and choose Maven > Update Project.

您可以让 m2eclipse 为您修复此配置,而不是手动修复此配置:只需右键单击该项目并选择Maven > Update Project

回答by dunni

And another point: JUnit test classes should be in src/test/java, not src/main/java, otherwise they aren't detected correctly by Maven as test classes and they would be included in the packaged jar and not in the test jar.

还有一点:JUnit 测试类应该在 src/test/java 中,而不是 src/main/java 中,否则它们不会被 Maven 正确检测为测试类,它们将包含在打包的 jar 中而不是在测试 jar 中.

回答by Pushkin

I faced same issue. Tried above suggestions of configuring output folder & Maven>Update Project but neither worked. Finally changed my testOutputDirectory to "build/classes" as well and now Unit Tests are getting picked up and executed.

我遇到了同样的问题。尝试了上述配置输出文件夹和Maven>更新项目的建议,但都没有奏效。最后将我的 testOutputDirectory 也更改为“构建/类”,现在单元测试正在被接收和执行。

Finally found the reason for the issue. In my pom we had also configured maven compiler plugin as below

终于找到问题的原因了。在我的 pom 中,我们还配置了 maven 编译器插件,如下所示

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <outputDirectory>build/classes</outputDirectory>
            </configuration>
       </plugin>

outputDirectory configuration is not needed and was the cause of above issue. After removing this tag, junits are getting compiled to build>testclasses folder and are being run during maven build as well. Yippee :)

不需要 outputDirectory 配置,这是导致上述问题的原因。删除此标记后,junit 将被编译到 build>testclasses 文件夹,并且也在 maven 构建期间运行。伊皮 :)

回答by developer

Please check "testSourceDirectory" path which can be configured in your pom.xml. And then, Add the folder (configured in "testSourceDirectory" path) to the eclipse build path.

请检查可以在您的 pom.xml 中配置的“testSourceDirectory”路径。然后,将文件夹(在“testSourceDirectory”路径中配置)添加到 eclipse 构建路径。

Please find the sample "testSourceDirectory" in pom.xml below:

请在下面的 pom.xml 中找到示例“testSourceDirectory”:

<build>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <plugins>
     <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>      
    </plugins>
  </build>

回答by ElliotMok

Make sure that is there any exclamation mark on your project icon! In my case, i ignored that there is a exclamation point like: exclamation point on project icon

确保您的项目图标上有任何感叹号!就我而言,我忽略了有一个感叹号,例如: 项目图标上的感叹号

Open the "Markers" perspective, then troubleshoot the problems according to the tips. what the "Markers" perspective show

打开“Markers”透视图,根据提示排查问题。 “标记”视角显示什么

The junit test classes can be execute successfully after i called "mvn clean test" because they are not refer the unreadable jar which be warned in "Markers" perspective.Therefor, it's easily to ignore it..

在我调用“mvn clean test”后,junit 测试类可以成功执行,因为它们不是指在“标记”角度警告的不可读的 jar。因此,很容易忽略它。

回答by Gulats

For someone working on java-scala mix project, this is something to note. Even after doing the configuration in the manner shown below,

对于从事 java-scala 混合项目的人来说,这是需要注意的。即使按照如下所示的方式进行配置后,

<build>
    <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
    <plugins>
    ...
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>3.3.2</version>
            <configuration>
                <testSourceDir>${basedir}/src/test/scala</testSourceDir>
                <testOutputDir>${basedir}/target/test-classes</testOutputDir>
            </configuration>
         </plugin>
    </plugins>
</build>

and doing a Maven > Update Project, eclipse honours the output directory of the src/test/java folder in the project, however, not for the src/test/scala folder. (You can figure this out by right-clicking on the specific source folder and choosing Build Path > Configure Output Folder...which should display the location as specified in the pom for the former case, however, not for the later case.

并执行Maven > Update Project,eclipse 尊重项目中src/test/java 文件夹的输出目录,但是,不适用于 src/test/scala 文件夹。(您可以通过右键单击特定的源文件夹并选择Build Path > Configure Output Folder... 来解决这个问题,这应该显示前一种情况的 pom 中指定的位置,但是,后一种情况则不然。

This is already a known bug for using scala with m2e as mentioned here: http://scala-ide.org/docs/tutorials/m2eclipse/

这已经是将 scala 与 m2e 一起使用的已知错误,如下所述:http://scala-ide.org/docs/tutorials/m2eclipse/

Warning

As of March 2013, a bug causes both src/main/scala and src/test/scala to use the default output folder (target/classes). This may be confusing >when building tests, since their class files will not end in target/test-classes, as they would when building on the command line. You can work around this by manually changing the output folder for src/test/scala.

警告

截至 2013 年 3 月,一个错误导致 src/main/scala 和 src/test/scala 使用默认输出文件夹(目标/类)。>在构建测试时这可能会令人困惑,因为它们的类文件不会像在命令行上构建时那样以目标/测试类结尾。您可以通过手动更改 src/test/scala 的输出文件夹来解决此问题。

回答by Jim Kiley

My problem wasn't the JUnit plugin but rather the configuration in my pom.xml.

我的问题不是 JUnit 插件,而是我的 pom.xml 中的配置。

After reviewing all the answers to this question, @Gulats's answer implied to me that I should try setting a testOutputDirectoryin my maven-compiler-plugin section, and that did the trick:

在查看了这个问题的所有答案后,@Gulats 的回答暗示我应该尝试testOutputDirectory在我的 maven-compiler-plugin 部分设置 a ,并且这样做了:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.8</source>
    <target>1.8</target>
    <testOutputDir>${basedir}/target/test-classes</testOutputDir>
  </configuration>
</plugin>

回答by James Scriven

Eclipse is not expecting anything else to be mucking with the class files. It assumes that if you haven't editted the file in eclipse it hasn't changed and doesn't need compiling. I think the issue stems from eclipse and maven sharing an output directory. I've often seen this if my mvn build fails, it will have deleted the class files as part of the clean but not compiled new ones. I think the best solution would be to have seperate build dirs for mvn and eclipse, but I've never look into this.

Eclipse 不希望类文件有任何其他问题。它假设如果您没有在 eclipse 中编辑过文件,它就没有改变,也不需要编译。我认为这个问题源于 eclipse 和 maven 共享一个输出目录。如果我的 mvn 构建失败,我经常看到这种情况,它将删除类文件作为干净但未编译的新文件的一部分。我认为最好的解决方案是为 mvn 和 eclipse 建立单独的构建目录,但我从来没有研究过这个。