program tip

junit의 java.lang.NoClassDefFoundError

radiobox 2020. 11. 1. 17:31
반응형

junit의 java.lang.NoClassDefFoundError


내 junit 테스트 코드에서 Java 에서이 오류가 발생합니다. 나는 인터넷을 찾아 봤는데 클래스 패스에 junit.jar를 추가해야한다고 말한다.

Eclipse에서 프로젝트 속성 창의 클래스 경로에 추가했지만 여전히 초기화 오류가 발생합니다. 어떡해 ..?

다음은 오류의 전체 추적입니다.

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 25 more

  1. 패키지 탐색기에서 프로젝트를 마우스 오른쪽 버튼으로 클릭하고 속성을 클릭합니다.
  2. Java Build Path> Libraries 탭으로 이동합니다.
  3. '라이브러리 추가'버튼을 클릭하십시오.
  4. JUnit 선택
  5. 다음을 클릭하십시오.
  6. 드롭 다운 버튼 JUnit4 또는 기타 새 버전을 선택합니다.
  7. 마침을 클릭하십시오.
  8. 그럼 좋아요.

  1. Eclipse-> 최상위 메뉴-> 실행-> 구성 실행
  2. 테스트의 모든 항목을 삭제하십시오. 테스트는 YourTest.Method_1 ()로 나타날 수 있습니다. 그것도 삭제하십시오.
  3. 다시 실행하십시오. Eclipse가 새로운 구성을 구축하게하십시오.

부록 : 로컬에서 "사용자 라이브러리"를 만들고 다음이있는 프로젝트에 추가했습니다.

hamcrest-core-1.3.jar

junit-4.12.jar


JUnit 웹 사이트에서 JUnit jar를 다운로드했지만 Hamcrest jar를 다운로드하는 것을 잊은 경우에도 동일한 문제가 발생할 수 있습니다. 둘 다 필요합니다 (둘 다 다운로드하라는 지침이 있지만 건너 뛰었습니다! 죄송합니다).


Maven에서 사용할 때 artifact junit : junit를 4.8.2에서 4.11로 업데이트하십시오.


Eclipse에서 아래 단계에 따라 위의 문제를 해결할 수있었습니다.

실행하려는 테스트 파일을 마우스 오른쪽 버튼으로 클릭하고 실행 도구-> 구성 실행-> 클래스 경로 탭 선택-> 부트 스트랩 항목 선택-> 고급 선택-> 라이브러리 추가 선택-> JUnit 선택-> 다음-> 드롭 다운-> 마침에서 JUnit4를 선택합니다.

그런 다음 적용-> 실행을 선택 하십시오.


The org/hamcrest/SelfDescribing class is not on the run-time classpath.


Try below steps:

  1. Go to your project's run configuration. In Classpath tab add JUnit library.
  2. Retry the same steps.

It worked for me.


I had the same issue, the problem was in the @ContextConfiguration in me test classes, i was loading the servlet context too i just change:

@ContextConfiguration(locations = { "classpath*:**\*-context.xml", "classpath*:**\*-config.xml" })

to:

@ContextConfiguration(locations = { "classpath:**\*-context.xml", "classpath:**\*-config.xml" })

and that´s it. this way im only loading all the files with the pattern *-context.xml in me test path.


  1. Make sure Environment variable JUNIT_HOME is set to c:\JUNIT.
  2. then In run configuration > select classpath > add external jars junit-4.11.jar

The reason for this is "hamcrest-core" jar is not in classpath as it doesn't comes directly with junit jar. So there are two ways to resolve this:

  1. select project -> buildpath -> add libraries and select junit (It contains both junit & hamcrest-core)
  2. download hamcrest-core from maven repo and add this to your classpath.

These steps worked for me when the error showed that the Filter class was missing (as reported in this false-diplicated question: JUnit: NoClassDefFoundError: org/junit/runner/manipulation/Filter ):

  1. Make sure to have JUnit 4 referenced only once in your project (I also removed the Maven nature, but I am not sure if this step has any influence in solving the problem).
  2. Right click the file containing unit tests, select Properties, and under the Run/Debug settings, remove any entries from the Launch Configurations for that file. Hit Apply and close.
  3. Right click the project containing unit tests, select Properties, and under the Run/Debug settings, remove any entries involving JUnit from the Launch Configurations. Hit Apply and close.
  4. Clean the project, and run the test.

Thanks to these answers for giving me the hint for this solution: https://stackoverflow.com/a/34067333/5538923 and https://stackoverflow.com/a/39987979/5538923).


Adding my two cents to other answers.

Check if you haven't by any chance created your test class under src/main/java instead of usual src/test/java. The former is the default in Eclipse when you create a new test class for whatever reason and can be overlooked. It can be as simple as that.


This error also comes if 2 versions of hamcrest-library or hamcrest-core is present in the classpath.

In the pom file, you can exclude the extra version and it works.


even Junit4.11.jar doesnot have the hamcrest-core.jar. I added explicitly in the classpath and the issue was resolved.


If you have more than one version of java, it may interfere with your program.

I suggest you download JCreator.

When you do, click configure, options, and JDK Profiles. Delete the old versions of Java from the list. Then click the play button. Your program should appear.

If it doesn't, press ctrl+alt+O and then press the play button again.

참고URL : https://stackoverflow.com/questions/4228047/java-lang-noclassdeffounderror-in-junit

반응형