可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I know this question is pretty general but I haven't found any hints on why this error may show up. What are possible causes of seeing initalizationError in Eclipse window? I get no useful information just a long and useless failure trace (not included here).
I am using JUnit 4.11
I have written the following code - just to see if it works:
package test;
import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class SimpleTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void test() {
assertEquals(15, 15);
}
}
Edit: Sorry In Eclipse window it's called actually a "Failure trace":
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing at
java.lang.ClassLoader.defineClass1(Native Method) 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$100(Unknown Source) at
java.net.URLClassLoader$1.run(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:10)
at
org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at
org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at
org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at
org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
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.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:
You've probably got one of two problems:
1) You're using JUnit 4.11, which doesn't include hamcrest. Add the hamcrest 1.3 library to your classpath.
2) You've got hamcrest 1.3 on your classpath, but you've got another version of either junit or hamcrest on your classpath.
For background, junit pre 4.11 included a cut down version of hamcrest 1.1. 4.11 removed these classes.
回答2:
For me it was a silly mistake. I inadvertently set the test as private instead of public:
@Test
private void thisTestWasCausingProblems() {
...
}
it should have been
@Test
public void thisTestIsOK() {
...
}
回答3:
Just try "Project > Clean..." - seems to be THE solution to many problems in Eclipse!
回答4:
For me it was a missing static
keyword in one of the JUnit annotated methods, e.g.:
@AfterClass
public static void cleanUp() {
// ...
}
回答5:
I received this error when the class was annotated with @Ignore and I tried to run a specific test via right clicking on it. Removing the @Ignore fixed the problem.
回答6:
I've experimented the exact same error. My problem was that the SetUp method I had created was declared as static.
If using eclipse, one could get a good description by clicking above the error and then checking the Failure trace window, just below... That's how I found the real problem!
回答7:
This problem also occurs if you have a private Rule in you class:
@Rule
private TemporaryFolder folderRule;
Make it public.
回答8:
Also make sure you have all @Before
-, @After
- and whatever-JUnit-annotated methods declared as public
. I had mine declared as private
which caused the issue.
回答9:
For me, it was due to the "return type" of the test method. It should be "void"
回答10:
For me it was something to do with commons-logging. Since I was using
@RunWith(SpringJUnit4ClassRunner.class)
This resolved my issue
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
回答11:
I had the same problem: Once it was excel path issue and other time it was missing @Test
annotation.
回答12:
I just had the same problem and the same message. I was able to get it fixed by importing to the Java Building Path the hamcrest-all-1.3.jar
archive. I'm using a JUnit 4.12 version.
回答13:
My mistake was that I missed out @Test annotation on the test method.
回答14:
I had the same issue... I was using maven building tool with JUnit dependency scope as 'test' and my @Test method was in main module.
To resolve, I've:
1. Commented the scope tag of JUnit dependency in the pom.xml
2. Rebuilt the package
3. Made sure the Test class has only one constructor
The issue can be two-folded:
1. The JUnit dependency is limited to test scope and the @Test method is in main scope
2. The test class has more than one constructor
回答15:
I had the same problem, the solution for me is the following.
I had only install the junit.jar file from the web, but this library related with the hacrest-core.jar
When I downloaded the hacrest-core.jar file and added it in my project everything works fine.
回答16:
For me the solution was one of the methods had to be void, I had it as Boolean.
回答17:
My problem was that my parent class had no @Test
methodes. I used there only some utilities. When I declared it abstract
it works.
回答18:
Junit 4.11 doesn't work with Spring Test framework.
Also this link InitializationError When Use Spring + Junit explains how to debug junit initalizationError in eclipse.
回答19:
If you're using the xtend language (or some other JVM lang with type inference) and haven't explicitly defined the return type then it may be set to a non-void type because of the last expression, which will make JUnit fail.
回答20:
I got another failure trace, but for future visitors using their favorite search engine:
For me the problem was that eclipse somehow decided to execute only a single method in my test class (I couldn't figure out why, though).
Especially if you did not change anything in your setup, check your run configuration.
回答21:
In my case I have changed project name in pom.xml so the new target directory was generated and junit was searching classes in old directory. You need to change directory for JUnit also in Eclipse
Properties -> Java Build Path -> Source (Default output folder)
And also check because some of source folders listed in the same window may have former directory name. You need to change them too.
回答22:
For me, I mistakenly added a parameter to the test method which caused initialization error.