Spring测试DBUnit:即使在最简单的测试中,表比较也会失败

时间:2012-10-05 05:35:17

标签: mysql dbunit spring-test

我使用Spring测试DBUnit来测试我的数据库服务和Hibernate。 我写了一个最简单的测试:

@Test
    @DatabaseSetup("dumbDataSample_DBAccount.xml")
    @DatabaseTearDown("dumbDataSample_DBAccount.xml")
    @ExpectedDatabase(assertionMode=DatabaseAssertionMode.NON_STRICT, value ="dumbDataSample_DBAccount.xml")
    public void testBasic() {
    }

它失败了!为什么?它在我修改表之前确实有效(删除了一些字段,添加了其他的,重新分配的主键)似乎DBunit比较了来自不同记录的companyName列 dumbDataSample_DBAccount.xml

    <?xml version="1.0" encoding="UTF-8"?>
<dataset>
    <adminaccounts username="naruto" forcePassChange="true" companyName="Konoha" email="naruto@konoha.jp"/>
    <adminaccounts username="sephiroth" forcePassChange="false" companyName="Shinra" email="sephi@shinra.com"/>
    <adminaccounts username="geralt" forcePassChange="false" companyName="no"/>
</dataset>

有什么想法吗? 这是跟踪

junit.framework.ComparisonFailure: value (table=adminaccounts, row=0, col=companyName) expected:<[Konoha]> but was:<[no]>
    at org.dbunit.assertion.JUnitFailureFactory.createFailure(JUnitFailureFactory.java:39)
    at org.dbunit.assertion.DefaultFailureHandler.createFailure(DefaultFailureHandler.java:105)
    at org.dbunit.assertion.DefaultFailureHandler.handle(DefaultFailureHandler.java:208)
    at org.dbunit.assertion.DbUnitAssert.compareData(DbUnitAssert.java:524)
    at org.dbunit.assertion.DbUnitAssert.assertEquals(DbUnitAssert.java:409)
    at org.dbunit.assertion.DbUnitAssert.assertEquals(DbUnitAssert.java:312)
    at org.dbunit.assertion.DbUnitAssert.assertEquals(DbUnitAssert.java:274)
    at org.dbunit.assertion.DbUnitAssert.assertEqualsIgnoreCols(DbUnitAssert.java:122)
    at org.dbunit.Assertion.assertEqualsIgnoreCols(Assertion.java:74)
    at com.github.springtestdbunit.assertion.NonStrictDatabaseAssertion.assertEquals(NonStrictDatabaseAssertion.java:44)
    at com.github.springtestdbunit.DbUnitRunner.verifyExpected(DbUnitRunner.java:128)
    at com.github.springtestdbunit.DbUnitRunner.afterTestMethod(DbUnitRunner.java:80)
    at com.github.springtestdbunit.DbUnitTestExecutionListener.afterTestMethod(DbUnitTestExecutionListener.java:141)
    at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:406)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:91)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

这是表格的脚本:

CREATE TABLE `adminaccounts` (
`username` varchar(50) NOT NULL,
`companyName` varchar(50) NOT NULL DEFAULT 'Unknown',
`email` varchar(50) DEFAULT NULL,
`forcePassChange` bit(1) NOT NULL DEFAULT b'1',
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

这是我的实体类:

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="adminaccounts")
public class DBAccount implements Serializable {
    private static final long serialVersionUID = 1L;
    /** Account name */
    @Id
    @Column(name="username")
    private String username;
    public void setUsername(String n)
    {
        this.username = n;
    }
    public String getUsername()
    {
        return this.username;
    }
    /** Force password change flag */
    @Column(name="forcePassChange")
    private boolean forceChange;
    public void setForceChange(boolean f)
    {
        this.forceChange = f;
    }
    public boolean getForceChange()
    {
        return this.forceChange;
    }
    /**  Company name */
    @Column(name="companyName")
    private String company;
    public void setCompany(String c)
    {
        this.company = c;
    }
    public String getCompany()
    {
        return this.company;
    }
    /** email */
    @Column(name="email")
    private String email;
    public void setEmail(String e)
    {
        this.email = e;
    }
    public String getEmail()
    {
        return this.email;
    }

    public String toString()
    {
        String s = this.username + " " + this.company + " " + this.email + " " + (this.forceChange ? "Force" : "No force");
        return s;
    }

}

hibernate config:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <mapping class="jp.co.rns.model.DBAccount" />
    </session-factory>

</hibernate-configuration>

我使用注释,因此没有persistence.xml文件

这是测试背景

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-3.1.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />    
    <!-- ===============Properties files================== -->
 <util:properties id="appProperties" 
    location="classpath:application.properties">
    </util:properties>
 <beans:bean id="propertyConfigurer"
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
     p:location="classpath:jdbc.properties" />
<!-- ================================================= -->

     <beans:bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="configLocation">
            <beans:value>classpath:hibernate.cfg.xml</beans:value>
        </beans:property>
        <beans:property name="configurationClass">
            <beans:value>org.hibernate.cfg.AnnotationConfiguration</beans:value>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">${jdbc.dialect}</beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

    <tx:annotation-driven />
    <beans:bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="sessionFactory" />
    </beans:bean>

       <!--   p:driverClassName="${jdbc.driverClassName}"-->
    <beans:bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
        p:password="${jdbc.password}" >
        <beans:property name="driverClassName">
        <beans:value>${jdbc.driverClassName}</beans:value>
        </beans:property>
        </beans:bean>
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
    <context:component-scan base-package="jp.co.rns" />

    <mvc:interceptors>
    <beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <beans:property name="paramName" value="lang" />
    </beans:bean>
    <beans:bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
        <beans:property name="paramName" value="lang" />
    </beans:bean>
    </mvc:interceptors>

    <beans:bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <beans:property name="basename" value="classpath:static" />
    <beans:property name="defaultEncoding" value="UTF-8"/>
</beans:bean>

<beans:bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <beans:property name="paramName" value="lang" />
</beans:bean>

<beans:bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <beans:property name="defaultLocale" value="en"/>
</beans:bean> 

<!-- For Themes -->
<beans:bean id="themeSource"
    class="org.springframework.ui.context.support.ResourceBundleThemeSource">
        <beans:property name="basenamePrefix" value="theme_" />
</beans:bean>

<!-- Theme Change Interceptor and Resolver definition -->
<beans:bean id="themeChangeInterceptor"
    class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
    <beans:property name="paramName" value="lang" />     
</beans:bean>
<beans:bean id="themeResolver"
    class="org.springframework.web.servlet.theme.CookieThemeResolver">
    <beans:property name="defaultThemeName" value="en" />    
</beans:bean>
<beans:bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <beans:property name="interceptors">
    <beans:list>            
        <beans:ref bean="themeChangeInterceptor" />
        <beans:ref bean="localeChangeInterceptor" />
    </beans:list>
    </beans:property>
</beans:bean>
</beans:beans>

1 个答案:

答案 0 :(得分:1)

我找到了原因:

@Id
@Column(name="username")
private String username;

主键必须是自动生成的int,而不是string;

我重组了我的桌子; DBUnit现在可以使用了