【单元测试】Intellij IDEA junit 使用之org.junit不存在

- 错误显示

在java中进行junit单元测试的时候,在给方法添加*@Test* 后,在IDED中运行时发生了**“程序包org:junit不存在”**错误,如下图所示:
【导包块】
【单元测试】Intellij IDEA junit 使用之org.junit不存在
【运用Test】
【单元测试】Intellij IDEA junit 使用之org.junit不存在
【运行报错】
【单元测试】Intellij IDEA junit 使用之org.junit不存在

- 解决办法
搭配两个jar包: 【junit-4.12.jar】 + 【hamcrest-core-1.3.jar】。

- 解决步骤
——[环境:Mac版 IDEA2020.1.3]

  1. 进入【Project Structure】
    点击下图中的标识进入。
    【单元测试】Intellij IDEA junit 使用之org.junit不存在

  2. 【+】 --> 【Java】
    【单元测试】Intellij IDEA junit 使用之org.junit不存在

  3. 选择【junit-4.12.jar】文件
    路劲:/Applications/IntelliJ IDEA.app/Contents/lib/junit-4.12.jar
    【单元测试】Intellij IDEA junit 使用之org.junit不存在

  4. 然后【Open】,如下图,最重要的,【Open】后一定一定要选择对应的模块(我的是day05…)
    【单元测试】Intellij IDEA junit 使用之org.junit不存在

  5. 如图,添加了一个【junit-4.121】(最后一位1,是我已经有一个【junit-4.12】存在,所以这里要清楚自己添加进来的是哪一个,不要混淆了),再点击下面的【+】号。
    【单元测试】Intellij IDEA junit 使用之org.junit不存在

  6. 导入【hamcrest-core-1.3.jar】包,路径同样的。
    【单元测试】Intellij IDEA junit 使用之org.junit不存在

  7. 查看导入的两个包,确定后【Apply】,【OK】
    【单元测试】Intellij IDEA junit 使用之org.junit不存在

  8. 报错消失,错误解决!
    【单元测试】Intellij IDEA junit 使用之org.junit不存在