Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUNIT Initialization Error [duplicate]

Tags:

java

junit

this is my code of file AllTests:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
    ElementTests.class
})
public class AllTest {}

ElementTests.java

import org.junit.Test;
import pl.polsl.lab1.Model.*;
import static org.junit.Assert.*;
import org.junit.Test;

public class ElementTests {

    @Test
    public void properSymbolPlayerTest()
    {
        //given
        ElementOfBoard element = new ElementOfBoard();
        ElementOfBoard element2 = new ElementOfBoard();
        //when
        element.setSymbol(1);
        element.setSymbol(2);
        //then
        assertEquals('O', element);
        assertEquals('X', element2);
    }  

}

My error

Please, how to solve it? I am using junit 4.11. I really have no idea what initialization error could be. Can you help me please?

My Stack trace:

org/hamcrest/SelfDescribing
java.lang.NoClassDefFoundError
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructor0(Class.java:3075)
    at java.lang.Class.getConstructor(Class.java:1825)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
like image 984
Aleksandra Bienioszek Avatar asked Nov 07 '15 16:11

Aleksandra Bienioszek


People also ask

How do I fix JUnit initialization error?

Show activity on this post. 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.

What is initialization error in Java?

An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable. As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism.


3 Answers

  • 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.

That should solve this problem.

like image 51
Biraj B Choudhury Avatar answered Oct 07 '22 06:10

Biraj B Choudhury


In my case the issue was I had accidentally commented out @Test before the method I was trying to test. So it was throwing the initialization error and method not available.

This can also happen if we have @Ignore on the class level.

like image 26
Vaishnavi R Avatar answered Oct 07 '22 07:10

Vaishnavi R


Please check @Test annotaion is added and imported from the right import path import org.junit.Test;

like image 1
Parthasarathy Deva Avatar answered Oct 07 '22 08:10

Parthasarathy Deva