我正在使用 maven、scala 和 junit 编写一个简单的项目。
我发现的一个问题是我的测试找不到 org.junit.framework.Test。测试文件:
import org.junit.framework.Test
class AppTest {
@Test
def testOK() = assertTrue(true)
@Test
def testKO() = assertTrue(false)
}
返回错误:
[WARNING]..... error: object junit is not a member of package org
[WARNING] import org.junit.framework.Test
[WARNING] ^
我确实添加了 junit 作为依赖项,它显然位于我的存储库中。
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
谁能告诉我这是什么原因造成的?
非常感谢
最佳答案
正如@nico_ekito所说,您的导入不正确,应该是org.junit.Test
。
但这并不能解释你的问题:
[WARNING]..... error: object junit is not a member of package org
[WARNING] import org.junit.framework.Test
[WARNING] ^
所以它找不到org.junit。这意味着当您编译测试时 org.junit 不在类路径中。所以要么是你的依赖树搞砸了,要么是实际的 jar 搞砸了。
尝试
mvn dependency:list
这应该会导致类似的结果:
[INFO] The following files have been resolved:
[INFO] junit:junit:jar:4.8.1:test
确保您没有解析其他 junit 库。如果一切正常,请检查 junit jar 的内容。它应该包含 org.junit.Test 类。如果没有,则说明您的存储库已损坏,或者您没有在正确的位置查找。了解这一点的最简单方法是使用:
mvn dependency:copy-dependencies
它使用依赖项的副本创建目标/依赖项。你可以看看那里的 jar 。
关于scala - Maven项目找不到junit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9487717/