java.lang.ClassNotFoundException:org.junit.runners.BlockJUnit4ClassRunner

时间:2014-12-08 15:41:29

标签: java eclipse spring junit

从Eclipse中执行Spring 3.2.8项目的Junit(junit-4.4)没有问题,(点击右键 - > Run As - > Junit Test)

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;


public class AccomodationTypeDaoTest extends BaseDaoTest {
    private AccomodationTypeDao accTypeDao;

    @Test
    public void testFindAll() {
        assertNotNull(accTypeDao.findAll());
    }

    @Autowired
    public void setAccomodationTypeDao(AccomodationTypeDao daoInstance) {
        this.accTypeDao = daoInstance;
    }

}



@SuppressWarnings("deprecation")
public class BaseDaoTest extends AbstractAnnotationAwareTransactionalTests {
    private static final String[] CONFIG_LOCATIONS = new String[] { "classpath:com/dao/testDataAccessContext.xml" };

    @Override
    protected String[] getConfigLocations() {
        return CONFIG_LOCATIONS;
    }

    @Test
    public void testBaseDaoTest() {
        assertTrue(true);
    }    
}

我也没有问题,对包中的所有类做同样的事情。

但是当我点击测试文件夹/ MyProject / MyProjectWeb / test / src时,我得到了这个例外:

java.lang.TypeNotPresentException: Type [unknown] not present
    at sun.reflect.annotation.TypeNotPresentExceptionProxy.generateException(TypeNotPresentExceptionProxy.java:46)
    at sun.reflect.annotation.AnnotationInvocationHandler.invoke(AnnotationInvocationHandler.java:75)
    at com.sun.proxy.$Proxy6.value(Unknown Source)
    at org.junit.internal.requests.ClassRequest.getRunnerClass(ClassRequest.java:49)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:28)
    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.NoClassDefFoundError: org/junit/runners/BlockJUnit4ClassRunner
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    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)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:270)
    at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114)
    at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
    at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
    at sun.reflect.annotation.AnnotationParser.parseSig(AnnotationParser.java:390)
    at sun.reflect.annotation.AnnotationParser.parseClassValue(AnnotationParser.java:371)
    at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:300)
    at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:241)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:88)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:70)
    at java.lang.Class.initAnnotationsIfNecessary(Class.java:3217)
    at java.lang.Class.initAnnotationsIfNecessary(Class.java:3224)
    at java.lang.Class.initAnnotationsIfNecessary(Class.java:3224)
    at java.lang.Class.getAnnotation(Class.java:3176)
    at org.junit.internal.requests.ClassRequest.getRunnerClass(ClassRequest.java:45)
    ... 9 more
Caused by: java.lang.ClassNotFoundException: org.junit.runners.BlockJUnit4ClassRunner
    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)
    ... 37 more

1 个答案:

答案 0 :(得分:4)

来自BlockJUnit4ClassRunner的{​​{3}}:

/**
 * [...]
 * </ul>
 *
 * @since 4.5
 */
public class BlockJUnit4ClassRunner

所以它在4.4中可用,您可能无需更改任何代码即可升级到4.11