未找到 JUnit 文件
我已将 JUnit jar 添加到我的类路径中,它可以使用命令窗口找到并运行测试,但是当我尝试编译我知道有效的测试时,它似乎找不到与 JUnit 有关的任何内容。每个测试类都会出现以下错误。在另一台电脑上测试过,可以用,但在我的电脑上不行。有什么想法吗?
EstateAgentTest.java:25: incompatible types
found : Test
required: java.lang.annotation.Annotation
@Test public void rentable1() {
多谢。
I've added the JUnit jar to my classpath, and it can find and run tests alright using the command window, but when I try and compile a test I know works, it can't seem to find anything to do with JUnit. I get the following error for every test class. Tested it on another computer and it works, but not on mine. Any ideas?
EstateAgentTest.java:25: incompatible types
found : Test
required: java.lang.annotation.Annotation
@Test public void rentable1() {
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我今天遇到了这个问题...
事实证明,调用我的类名“Test.java”是一个坏主意 - 它与测试方法上方的 @Test 行冲突。解决方法:
I encountered this problem today...
Turns out calling my class name "Test.java" was a bad idea - it clashes with the @Test line above your test method. To fix:
对于帖子的创建者来说可能有点晚了,但对于遇到同样问题的其他人来说,我遇到了完全相同的问题,并通过显式添加
import org.junit.Test;
解决了它,所以我的导入现在看起来像这样:我不知道为什么它不能仅使用
import org.junit.*;
工作。This is probably a little late for the creator of the post, but for others that encounter the same problem, I had the exact same problem, and solved it with explicitly adding
import org.junit.Test;
, so my imports now look like this:I have no idea why it didn't work with just
import org.junit.*;
.它不起作用的机器似乎有旧版本的 Java,没有注释功能。
您可以通过在命令行中输入 java -version 来轻松测试。大概是 1.4 左右,注释至少需要 1.5。
The machine where it doesn't work seems to have older version of Java without annotations feature.
You can easily test that by entering
java -version
to your command line. It's probably 1.4 or so and you need at least 1.5 for annotations.我不知道这是否会有所不同,但我习惯于在测试方法声明上方看到 @Test 注释,如下所示:
我的第二个猜测是,还有另一个用于 Test 的类文件以某种方式导入,该文件不是JUnit 注释。
I don't know if it'll make a difference, but I'm used to seeing the @Test annotation above the test method declaration, like this:
My second guess is that there's another class file for Test imported somehow that is not the JUnit annotation.
似乎您周围有
Test
类并且它已被导入。尝试此代码以确保您正在使用 jUnitTest
注释:Seems you have
Test
class somewhere around and it was imported. Try this code to be sure you are working with jUnitTest
annotation: