1

我正在尝试进行基本测试:

import org.junit.Test;
import org.unitils.UnitilsJUnit4;

public class SampleUnitilsTest extends UnitilsJUnit4 {

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

}

但我得到以下堆栈跟踪:

org.unitils.core.UnitilsException: An exception occured during the loading of core module hibernate with module class name org.unitils.orm.hibernate.HibernateModule
    at org.unitils.core.ModulesLoader.createAndInitializeModules(ModulesLoader.java:154)
    at org.unitils.core.ModulesLoader.loadModules(ModulesLoader.java:121)
    at org.unitils.core.Unitils.createModulesRepository(Unitils.java:222)
    at org.unitils.core.Unitils.init(Unitils.java:118)
    at org.unitils.core.Unitils.init(Unitils.java:105)
    at org.unitils.core.Unitils.initSingletonInstance(Unitils.java:74)
    at org.unitils.core.Unitils.getInstance(Unitils.java:52)
    at org.unitils.UnitilsJUnit4TestClassRunner.getUnitils(UnitilsJUnit4TestClassRunner.java:214)
    at org.unitils.UnitilsJUnit4TestClassRunner.getTestListener(UnitilsJUnit4TestClassRunner.java:204)
    at org.unitils.UnitilsJUnit4TestClassRunner.run(UnitilsJUnit4TestClassRunner.java:67)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    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: org.unitils.core.UnitilsException: Could not load class with name org.hibernate.cfg.AnnotationConfiguration
    at org.unitils.util.ReflectionUtils.getClassWithName(ReflectionUtils.java:482)
    at org.unitils.orm.hibernate.HibernateModule.init(HibernateModule.java:86)
    at org.unitils.core.ModulesLoader.createAndInitializeModules(ModulesLoader.java:151)
    ... 15 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.AnnotationConfiguration
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at org.unitils.util.ReflectionUtils.getClassWithName(ReflectionUtils.java:479)
    ... 17 more

我的 POM 包括以下内容:

    <dependency>
        <groupId>org.unitils</groupId>
        <artifactId>unitils-core</artifactId>
        <version>3.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.unitils</groupId>
        <artifactId>unitils-dbunit</artifactId>
        <version>3.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.unitils</groupId>
        <artifactId>unitils-orm</artifactId>
        <version>3.1</version>
        <scope>test</scope>
    </dependency>

如果这很重要,我会在 Eclipse Helios SR2 JUnit 4 测试器中执行此操作。有任何想法吗?

4

1 回答 1

3

巧合的是,我也试图为我的数据库测试选择单位。我能够用 maven 2.2.1 复制你的问题。根据我之前使用 maven 和 hibernate 的工作经验以及站点文档,这些依赖项应该就是您所需要的。

如果有什么问题,很可能是与hibernate相关的pom定义,果然在unitils-orm的pom中,以下依赖被设置为可选:

    <!-- Optional dependencies -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate</artifactId>
        <version>3.2.5.ga</version>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.3.1.ga</version>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>concurrent</groupId>
        <artifactId>concurrent</artifactId>
        <version>1.3.4</version>
        <optional>true</optional>
    </dependency>

添加 hibernate-entitymanager 确实解决了问题至于为什么 pom 是这样定义的,任何有效的原因只有开发人员知道..

于 2011-06-16T16:37:52.883 回答