6

使用 Spring 进行单元测试的最佳实践方法是什么?我认为 TestNG 和 jmockit 与 Spring 3 的组合还不错,所以这就是我现在正在做的事情,但是如果我不在为我的新 Spring 项目选择工具,请马上告诉我。:-)

无论如何,我已经创建了一个我想要测试的实体,但我不确定如何从 Spring 上下文中实际运行 TestNG。我创建了一个简单的测试类

package com.mydomain.data.entities.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.*;

@ContextConfiguration(locations={"classpath:applicationContext.xml"}) 
public class SimpleTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private ApplicationContext applicationContext;

    @BeforeClass
    protected void setUp() throws Exception {
        Assert.assertNotNull(applicationContext);
    }

    @Test
    public void testNothing() {

    }

}

使用 applicationContext.xml 导入 bean 来设置模型和业务层。首先,我希望它使用来自 Project/WebRoot/WEB-INF 的 applicationContext.xml,而此测试的源位于 Project/src/com/mydomain/data/entities/test 中,但我相信/applicationContext.xml 会给我 Projects/src,与类路径相同:这是正确的吗?

此外,既然我正在使用我的 Web 应用程序将使用的模型和业务层,我应该期望它的行为类似。但是,当我在测试中启动 TestNG 时,它说:类 org.hibernate.cfg.ExtendedMappings 具有接口 org.hibernate.cfg.Mappings 作为超类:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/Users/niklas/Documents/Eclipse/Project/WebRoot/WEB-INF/Project-Model.xml]: Invocation of init method failed; nested exception is java.lang.IncompatibleClassChangeError: class org.hibernate.cfg.ExtendedMappings has interface org.hibernate.cfg.Mappings as super class
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1393)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:289)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:286)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:352)
    at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:266)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:121)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.<init>(PersistenceExceptionTranslationInterceptor.java:77)
    at org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor.<init>(PersistenceExceptionTranslationAdvisor.java:70)
    at org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:99)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1414)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1381)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    ... 38 more
Caused by: java.lang.IncompatibleClassChangeError: class org.hibernate.cfg.ExtendedMappings has interface org.hibernate.cfg.Mappings as super class
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:700)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:254)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:399)
    at org.hibernate.cfg.AnnotationConfiguration.createExtendedMappings(AnnotationConfiguration.java:187)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:277)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1162)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:667)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1452)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1390)
    ... 53 more

这是什么,为什么我会得到这个?有什么线索吗?

我的 sessionFactory 看起来像这样

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="annotatedClasses">
      <list>
       <!-- Entity classes from package com.mydomain.data.entities -->
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.show_sql">false</prop>
      </props>
    </property>
  </bean>
4

5 回答 5

7

您的测试 ckass 需要扩展AbstractTestNGSpringContextTests。或者,如果您的代码正在测试数据库访问另一个有用的类来扩展是AbstractTransactionalTestNGSpringContextTests

于 2009-09-30T11:47:45.017 回答
5

现在问题似乎与 Spring 或 TestNG 中的任何一个都无关。关键部分是:

 java.lang.IncompatibleClassChangeError: class org.hibernate.cfg.ExtendedMappings has interface org.hibernate.cfg.Mappings as super class

你确定你的 CLASSPATH 没问题吗?即你有 Hibernate 模块的兼容版本并且那里没有重复?

如果您创建一个执行以下操作的简单 TestNG 测试会怎样:

Class.forName("org.hibernate.cfg.ExtendedMappings");

?

于 2009-09-30T13:11:08.740 回答
1

请参阅 Spring 文档中有关 TestNG 的章节:

于 2009-09-30T11:47:20.650 回答
1

对我来说,这个问题是因为我有不同的休眠版本,包括我自己,而不是因为包括他们在内的第三方。扫描你的项目/pom 以确保你自己只使用一个休眠版本是值得的。

于 2011-06-03T06:10:50.947 回答
0

您可能想尝试 hibernate-annotations-3.5.0-Beta-1,因为它与 hibernate-core-3.5.0-Beta-1 一致。

有时我仍然会得到 java.lang.IncompatibleClassChangeError,因为我正在尝试使用使用“旧”注释的休眠搜索。

此外,不确定您是否正在运行 JBoss,但如果是,您将在核心 3.5.0 中遇到此错误。

于 2009-11-01T03:18:19.980 回答