java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing


下载了最新的JUnit4版本,是4.13.2,结果尝试使用发现总是报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing这样的错误,

  • 一般的解决方案是,换一个低一点的版本就好了
  • 还有人说,是缺少hamcrest的包。去官网又看了一下,结果发现这样一段话:

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开始JUnit中没有hamcrest包了

JUnit官网

https://junit.org/junit5/

https://junit.org/junit4/

解决方案

  • 换成junit-4.10.jar
  • junit-4.13.2.jar + hamcrest-core-1.3.jar

其它JUnit使用参考

Java使用Junit4.jar进行单元测试 (https://zhuanlan.zhihu.com/p/430578155)