解決junit單元測試遇到java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing怎麼解決?

今天在寫代碼的時候想把老系統裏面加上單元測試,因此用最近作的springmvc上的一個項目中的junit扒一個下來:junit-4.12.jar 可是很奇怪在原來系統中好好能運行的,放到如今的項目中就總是報錯:java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing。java

瘋掉了後來查發現有人說換一個低版本的就好了,引入junit4.10.jar。果真行了,可是咱們要知其然更要只其因此然spring

查官網: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.mvc

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
複製代碼
相關文章
相關標籤/搜索