Eclipse JUnit - possible causes of seeing "initializationError" in Eclipse window

JavaEclipseJunit

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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() {
...
}

Solution 3 - Java

Just try "Project > Clean..." - seems to be THE solution to many problems in Eclipse!

Solution 4 - Java

For me it was a missing static keyword in one of the JUnit annotated methods, e.g.:

@AfterClass
public static void cleanUp() {
    // ...
}

Solution 5 - Java

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.

Solution 6 - Java

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!

Solution 7 - Java

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.

Solution 8 - Java

This problem also occurs if you have a private Rule in you class:

@Rule
private TemporaryFolder folderRule;

Make it public.

Solution 9 - Java

For me, it was due to the "return type" of the test method. It should be "void"

Solution 10 - Java

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>

Solution 11 - Java

I had the same problem: Once it was excel path issue and other time it was missing @Test annotation.

Solution 12 - Java

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.

Solution 13 - Java

My problem was that my parent class had no @Test methodes. I used there only some utilities. When I declared it abstract it works.

Solution 14 - Java

My mistake was that I missed out @Test annotation on the test method.

Solution 15 - Java

In my case, I had the following import in my test case:

import org.junit.jupiter.api.Test;

The correct import is:

import org.junit.Test;

Don't just import any old Test type from junit, make sure you pick the correct one.

Solution 16 - Java

Just if this helps, I was using Junit5 but the @RunWith(SpringRunner.class) was pointing towards import org.springframework.test.context.junit4.SpringRunner, which was messing around with things and hence was giving the initialization error. I fixed this and it worked.

Solution 17 - Java

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

Solution 18 - Java

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.

Solution 19 - Java

For me the solution was one of the methods had to be void, I had it as Boolean.

Solution 20 - Java

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.

Solution 21 - Java

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.

Solution 22 - Java

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.

Solution 23 - Java

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.

Solution 24 - Java

For me, I mistakenly added a parameter to the test method which caused initialization error.

Solution 25 - Java

I had naively added junit-4.12.jar from poi.apache.org to my Build Path in Eclipse. I deleted it (Project, Properties, Java Build Path, Libraries, junit-4.12.jar, Remove) then in my test class allowed Eclipse to "Fix Project Setup" after pressing Ctrl-1. No more problems! :-)

Solution 26 - Java

In my case, it was because I was not pointing to the correct package where I kept my feature (cucumber) file(s). This was a case of wrong path specification. See code snippet below:

import org.junit.runner.RunWith;

import io.cucumber.junit.CucumberOptions;
import io.cucumber.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
		
	features = "src/test/features",
	glue = "stepDefinitions"
		
		)

Below is the screenshot of the JUnit error: Junit Runner Error

Below is the Stack Trace in the console:

enter image description here

The final solution was I had to change the path to the correct package where my feature files were kept.

See the corrected code snippet below:

package cucumberOptions;

import org.junit.runner.RunWith;

import io.cucumber.junit.CucumberOptions;
import io.cucumber.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
		
	features = "src/test/java/features",
	glue = "stepDefinitions"
		
		)

public class TestRunner {

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlixonnView Question on Stackoverflow
Solution 1 - JavaMatthew FarwellView Answer on Stackoverflow
Solution 2 - JavaJACHView Answer on Stackoverflow
Solution 3 - JavaHenrik SchulzeView Answer on Stackoverflow
Solution 4 - JavafriederbluemleView Answer on Stackoverflow
Solution 5 - JavaLukeView Answer on Stackoverflow
Solution 6 - Javapablo.vixView Answer on Stackoverflow
Solution 7 - JavaMonkey SupersonicView Answer on Stackoverflow
Solution 8 - JavaT3rm1View Answer on Stackoverflow
Solution 9 - JavaKarthik MuruganView Answer on Stackoverflow
Solution 10 - JavaAnand RockzzView Answer on Stackoverflow
Solution 11 - JavayogendraView Answer on Stackoverflow
Solution 12 - JavaJuanJoRomanView Answer on Stackoverflow
Solution 13 - JavaunreinView Answer on Stackoverflow
Solution 14 - Javauser55926View Answer on Stackoverflow
Solution 15 - JavasgfView Answer on Stackoverflow
Solution 16 - JavaDivasView Answer on Stackoverflow
Solution 17 - JavaTony PuthenveettilView Answer on Stackoverflow
Solution 18 - JavaKostasAView Answer on Stackoverflow
Solution 19 - JavaPaNView Answer on Stackoverflow
Solution 20 - Javajeffery.yuanView Answer on Stackoverflow
Solution 21 - JavaAykut KllicView Answer on Stackoverflow
Solution 22 - JavaQw3ryView Answer on Stackoverflow
Solution 23 - JavaDawid DView Answer on Stackoverflow
Solution 24 - JavaStanleyView Answer on Stackoverflow
Solution 25 - Javai2BView Answer on Stackoverflow
Solution 26 - Javasamson ojoView Answer on Stackoverflow