使用 * 通配符进行 Junit 导入
我注意到导入 JUnit 时,* 通配符并不总是有效。
例如,对于注释@Test,您必须导入org.junit.Test,因为org.junit.* 无法识别该注释。
这是有什么原因吗,需要设置什么吗?或者只是像 JUnit 这样的东西的一个怪癖。
仅供参考,我正在使用:Junit 4.6、Intelli-J 8.1.3。
I've noticed that when importing JUnit, the * wildcard doesn't always work.
e.g. for the annotation @Test you must import org.junit.Test since org.junit.* doesn't recognize the annotation.
Is there a reason for this, is it something that needs setting? or just a quirk in the way somethings like JUnit are.
FYI, I am using: Junit 4.6, Intelli-J 8.1.3.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据您上面的评论:
在我看来,这可能是名称冲突。您是否从其他地方导入名为 Test 的类或接口?与您遇到问题的包位于同一包中是否存在名为 Test 的类? Java 可能看到的是其中之一而不是注释。
Based on your comment above:
it sounds to me like it could be a name collision. Are you importing a class or interface named Test from somewhere else? Is there a class named Test in the same package as the one where you're having the problem? It could be that Java is seeing one of these instead of the annotation.
我正在阅读 的内容http://www.velocityreviews.com/forums/t369296-p2-disadvantage-of-using-wildcards-in-import-statement.html表明IntelliJ中有一个“优化导入”设置可能与对此。
I'm reading something at http://www.velocityreviews.com/forums/t369296-p2-disadvantage-of-using-wildcards-in-import-statement.html that suggests that there's an "optimize imports" setting in IntelliJ that might relate to this.
我不知道为什么导入 org.junit.* 不会让您访问 org.junit.Test。事实上,我刚刚在 Eclipse 中尝试过,并且它可以工作。也许这是你的 IDEA 工作区的问题?
There's no reason I know of why importing org.junit.* wouldn't give you access to org.junit.Test. In fact, I just tried it in Eclipse, and it works there. Perhaps it's a problem with your IDEA workspace?
我今天在 Eclipse 中遇到了类似的问题。我对 org.junit.Assert.assertEquals 进行了静态导入,但是 org.junit.Assert.assert 的静态导入失败了!而且他们还是同一个班的!
我敢打赌这是一个 Eclipse 错误。我正在使用 junit 4.4 和 eclipse 3.5
I had a similar problem today in Eclipse. I made a static import to org.junit.Assert.assertEquals, but a static import of org.junit.Assert.assertThat fails! And they are in the same class!
I'll bet it's an Eclipse bug. I'm using junit 4.4 and eclipse 3.5
我不这样做,但使用
import org.junit.*;
在这里工作正常,以下测试打开绿灯:在命令行上使用 Java 6u16 进行测试,在 Eclipse 3.5 下,在IntelliJ IDEA 9.0 BETA CE。按预期到处工作。
替代文本 http://img18.imageshack.us/img18/7906/screenshotmavenpowermoc.png< /a>
I don't do it, but using
import org.junit.*;
works fine here, the following test turns on a green light:Tested with Java 6u16 on the command line, under Eclipse 3.5, under IntelliJ IDEA 9.0 BETA CE. Works everywhere as expected.
alt text http://img18.imageshack.us/img18/7906/screenshotmavenpowermoc.png