在IDEA里面已经新建好了一个类,并加入了内容,然后创建测试类,快捷键(Ctrl+Alt+T)或者如图右键 Goto Test创建后运行报错:
Error:(3, 24) java: 程序包org.junit不存在 : Cannot resolve symbol 'junit'
解决:
File -> Project Struct… -> Libraies -> 点击绿色的加号 -> Java -> 找到 IDEA 安装路径下的 Lib 中的junit-4.12 -> 确定完就行了,点击OK就出去了
但是,在接下来的测试中,却发现报错java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
查官网:
JUnit now uses the latest version of Hamcrest. Thus, you can use all the available matchers and benefit from an improved assertThat which will now print the mismatch description from the matcher when an assertion fails.
junit.jar: Includes the Hamcrest classes. The simple all-in-one solution to get started quickly.Starting with version 4.11, Hamcrest is no longer included in this jar.
junit-dep.jar: Only includes the JUnit classes but not Hamcrest. Lets you use a different Hamcrest version.
注意黑色加下划线的部分意思是4.11以上版本不在包含hamcrest。
所以现在有两个办法解决:
1.junit版本降到4.10
2.导入hamcrest-core-1.3.jar
导入hamcrest-core-1.3.jar:
- 找到junit
- 右键编辑
- 最左边+号
- 找到hamcrest-core-1.3.jar //在IDEA安装目录下的lib中
- 大功告成