java.lang.NoClassDefFoundError in junit
- Right click your project in Package Explorer > click Properties
- go to Java Build Path > Libraries tab
- click on 'Add Library' button
- select JUnit
- click Next.
- select in dropdown button JUnit4 or other new versions.
- click finish.
- Then Ok.
- Eclipse -> Top menu -> Run -> Run Configurations
- Delete all the occurrences of your test. Your test may appear as YourTest.Method_1(). Delete that as well.
- Re-run. Let Eclipse build a fresh configuration.
Addendum: Locally I have created a "User Library" and added to my projects which has
hamcrest-core-1.3.jar
junit-4.12.jar
The same problem can occur if you have downloaded JUnit jar from the JUnit website, but forgotten to download the Hamcrest jar - both are required (the instructions say to download both, but I skipped ahead! Oops)
When using in Maven, update artifact junit:junit from e.g. 4.8.2 to 4.11.
On Eclipse I was able to solve the above issue by following the below steps :
Right-click on the test file which you want to run, Select Run As -> Run Configurations -> Select Classpath tab -> Select to the bootstrap Entries -> Select Advanced -> Select Add library -> Select JUnit -> Next ->Select JUnit4 from the drop-down -> Finish
Then Select Apply -> Run