[java] Eclipse JUnit - possible causes of seeing "initializationError" in Eclipse window

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

This question is related to java eclipse junit

The answer is


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!


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.


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>

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


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

@Rule
private TemporaryFolder folderRule;

Make it public.


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.


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.


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


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


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.


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.


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.


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.


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

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.


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


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

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

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


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


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


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! :-)


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


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.


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.


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 {

}

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to junit

Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to resolve Unneccessary Stubbing exception JUnit 5: How to assert an exception is thrown? How do I mock a REST template exchange? Class Not Found: Empty Test Suite in IntelliJ Unable to find a @SpringBootConfiguration when doing a JpaTest Failed to load ApplicationContext (with annotation) Example of Mockito's argumentCaptor Mockito - NullpointerException when stubbing Method Spring jUnit Testing properties file