Eclipse / Maven:JUnit测试在运行时未编译 [英] Eclipse/Maven: JUnit tests not compiled when running them

查看:153
本文介绍了Eclipse / Maven:JUnit测试在运行时未编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



我真的无法弄清楚是什么错误。



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



我必须通过Maven编译并运行它们吗?为什么当我要运行它们时Eclipse不会编译文件? (有趣的是,有时候,这样做有时候一切都很完美)。

解决方案

我有同样的问题使用STS Eclipse(Spring开发变体),m2e和JUnit。解决方案是将 src / test / java 的输出文件夹设置为 target / test-classes


  1. 右键单击包资源管理器中的 src / test / java 文件夹

  2. 选择 构建路径 - > 配置输出文件夹

  3. 输入 target / test-classes ,点击 确定

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



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



这个问题有一些重复:




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

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 ClassNotFoundExceptions in Eclipse. I then have to manually recompile them by using mvn test-compile or other goals.

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

I really can't figure out what is wrong.

The JUnit java files are within src/main/java and are correctly named (*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, sometimes it does. Sometimes everything works perfectly.)

解决方案

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/java to target/test-classes:

  1. Right click the src/test/java folder in the Package Explorer
  2. Select Build Path -> Configure Output Folder
  3. Enter target/test-classes, click OK

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

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

There are a few duplicates to this question:

这篇关于Eclipse / Maven:JUnit测试在运行时未编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆