Intellij IDEA 添加 Junit4.12 以及报错解决

本文详细介绍了在IntelliJ IDEA中遇到JUnit测试错误时的解决方案,包括添加junit库和hamcrest-core-1.3.jar以解决'程序包org.junit不存在'及'java.lang.NoClassDefFoundError:org/hamcrest/SelfDescribing'等问题。

在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:

  1. 找到junit
    在这里插入图片描述
  2. 右键编辑
    在这里插入图片描述
  3. 最左边+号
  4. 找到hamcrest-core-1.3.jar //在IDEA安装目录下的lib中
  5. 大功告成
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值