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

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

问题描述

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

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

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

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.

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

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.

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

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

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

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.)

推荐答案

我在使用 STS Eclipse(Spring 开发变体)、m2e 和 JUnit 时遇到了同样的问题.解决方案是将 src/test/java 的输出文件夹设置为 target/test-classes:

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. 右键单击包资源管理器中的 src/test/java 文件夹
  2. 选择构建路径 -> 配置输出文件夹
  3. 输入target/test-classes,点击OK

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

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

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

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.

这个问题有几个重复:

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

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