6

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)
Andy Guibert
  • 41,446
  • 8
  • 38
  • 61

3 Answers3

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

  • Sorry, still not working... – Aleksandra Bienioszek Nov 07 '15 at 17:05
  • 1
    The SA question that I think is a duplicate of yours states that you need both the JUnit and the Hamcrest jars. Can you confirm that both are in your classpath? – Greg Krimer Nov 08 '15 at 06:38
  • @Greg Krimer, I have the same issue and i have junit.12.0 and hamcrest-all-1.3.jar on the build path – Raulucco Nov 15 '15 at 13:23
  • After several attemps I just removed junit from the build path and added it again. Now is working 8-P – Raulucco Nov 15 '15 at 13:28
  • Careful, sometimes you might start seeing [this](https://stackoverflow.com/q/2877262/234110) after adding JUnit to Library. in my case it was `java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package` – Anand Rockzz May 10 '19 at 15:06
2

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.

abarisone
  • 3,707
  • 11
  • 35
  • 54
2

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

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40