1

I'm getting below exception when Debugging my application using Junit

IDE Used-Eclipse Mar

Application Details-

JDK-1.7,Spring4 and Junit4 and hamcrest-all-1.3.jar

CodeBase-

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.app.api.facade.LoginServiceFacade;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/test-spring.xml")
public class LoginApi4JunitTest {
    @Autowired
    private LoginServiceFacade loginServiceFacade;

    @Test
    public void testIsUserExist()throws Exception{
        Boolean isUserExist=loginServiceFacade.isUserExist("xyz");
        Assert.assertEquals(Boolean.TRUE, isUserExist);
    }

}

Exception-

java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter
     at java.lang.Class.forName0(Native Method)
     at java.lang.Class.forName(Class.java:191)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadTestLoaderClass(RemoteTestRunner.java:320)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:310)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:305)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:283)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:207)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:191)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.manipulation.Filter
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 8 more

I already added Junit4 and hamcrest-all-1.3.jar in myBuild path.

pintu
  • 331
  • 4
  • 7
  • Please provide a code which cause this problem. Do you use some JUnit test Filter? See http://stackoverflow.com/questions/1230706/running-a-subset-of-junit-test-methods – bodo Jan 06 '16 at 16:47
  • Code added and not using any filter........Provided post is not helpful in my case. – pintu Jan 06 '16 at 17:44
  • Ok. You have written that, you got the problem, when you trying to debug JUnit test (Debug in Eclipse?). And when you run the tests (Run Eclipse?), then the problem doesn't appear? – bodo Jan 06 '16 at 18:46
  • In both cases we have got same error – pintu Jan 06 '16 at 18:55
  • Ok. Could you please post a Project buildpath Libraries? – bodo Jan 06 '16 at 19:22
  • Sorry for updating delayed yesterday it was fixed after adding the Junit4 and hamcrest-all-1.3.jar in classpath of Junit.Its not taking the values from java build path as i initially added. – pintu Jan 07 '16 at 17:18
  • Np. I'm glad that it's working. – bodo Jan 07 '16 at 19:20

3 Answers3

2

Try to remove the Eclipse launcher of the test, and run it again.

Go to Run/Run configurations/JUnit, select the launcher and click on "Remove". Then run the test again.

Juan Ignacio Barisich
  • 2,061
  • 19
  • 21
0

As have seen this when there is a broken/missing maven container. Edit .classpath and remove the entry:

classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"

Mark Z
  • 31
  • 2
0

Got this error when attempting to run a spring boot test.

It turned out that the test class was mistakenly in src/main/java and not in src/test/java.

xilef
  • 2,199
  • 22
  • 16