DBUnit + Spring + Hibernate:org.dbunit.dataset.NoSuchTableException:newsletter

时间:2015-02-26 10:38:08

标签: spring hibernate exception dbunit

我有一个问题,我已经工作了几天。

我使用Spring和Hibernate。直到现在还没有数据库访问的单元测试,所以我决定尝试让DBUnit启动并运行,但我一直收到这个错误:

org.dbunit.dataset.NoSuchTableException: newsletter
at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:288)
at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:109)
at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
at com.github.springtestdbunit.DbUnitRunner.setupOrTeardown(DbUnitRunner.java:162)
at com.github.springtestdbunit.DbUnitRunner.beforeTestMethod(DbUnitRunner.java:60)
at com.github.springtestdbunit.DbUnitTestExecutionListener.beforeTestMethod(DbUnitTestExecutionListener.java:160)
more...

这是我的抽象测试类:

package integration.databaseDaoTest;

import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-hibernate-db-integration-test.xml"})
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class,
    TransactionDbUnitTestExecutionListener.class })
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback= false)
public abstract class AbstractDBUnitTest {
}

我正在尝试运行的测试:

package integration.databaseDaoTest;

import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.DatabaseTearDown;

import dk.finanswatch.contentapi.repository.baseDao.JobAdsNewsletterBaseDao;
import dk.finanswatch.contentapi.testInterfaces.IntegrationTest;
import org.apache.xpath.SourceTree;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Category(IntegrationTest.class)

public class NewsletterDaoTest extends AbstractDBUnitTest {

    @Autowired
    private JobAdsNewsletterBaseDao dao;

    @Test
    @Transactional
    @DatabaseSetup("/datasets/newsletterDaoTest/newsletters.xml")
    //@DatabaseTearDown("/dataset/clearDB.xml")
    public void testIfDatasetIsEmpty() {
        System.out.println(dao);
    }
}

这是数据集newsletter.xml:

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
    <newsletter id="12" timestamp="2015-02-20 00:00:00.000000000" jobAdIds="teststring" />
</dataset>

还有alterSchema文件:

ALTER SCHEMA PUBLIC RENAME TO "watch_contentapi";

最后是应用程序上下文:

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

       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
              <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
              <property name="url" value="jdbc:hsqldb:mem:watch_contentapi" />
              <property name="username" value="sa" />
              <property name="password" value="" />
       </bean>

       <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
              <property name="dataSource" ref="dataSource"/>
              <property name="packagesToScan" value="dk.finanswatch.contentapi.model,dk.finanswatch.contentapi.security" />
              <property name="hibernateProperties">
                     <props>
                            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
                            <prop key="hibernate.show_sql">true</prop>
                            <prop key="hibernate.connection.autocommit">false</prop>
                            <prop key="hibernate.cache.use_second_level_cache">false</prop>
                            <prop key="hibernate.hibernate.cache.use_query_cache">true</prop>
                            <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
                            <!--You know nothing IntelliJ-validation-->
                            <prop key="hibernate.jdbc.factory_class">org.hibernate.jdbc.NonBatchingBatcherFactory</prop>
                     </props>
              </property>
       </bean>

       <bean id="dataSourceInitializer" class="org.springframework.jdbc.datasource.init.DataSourceInitializer">
              <property name="dataSource" ref="dataSource" />
              <property name="databasePopulator">
                     <bean class="org.springframework.jdbc.datasource.init.ResourceDatabasePopulator">
                            <property name="continueOnError" value="false" />
                            <property name="scripts">
                                   <list>
                                          <value>classpath:alterSchema.script</value>
                                   </list>
                            </property>
                     </bean>
              </property>
       </bean>

       <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
              <property name="sessionFactory" ref="sessionFactory" />
       </bean>


       <bean id="jobadsNewsletterBaseDao" class="dk.finanswatch.contentapi.repository.baseDao.JobAdsNewsletterBaseDaoImpl"/>

       <bean id="jobAdsNewsletterRepository" class="dk.finanswatch.contentapi.repository.impl.JobAdsNewsletterRepositoryImpl"/>

</beans>

对于长代码段,我们深表歉意。无论如何,我一直在四处寻找,但我还没能找到解决方案。如果我注释掉行@DatabaseSetup(&#34; /datasets/newsletterDaoTest/newsletters.xml")一切正常,我可以打印DAO的字符串表示,所以看起来我的上下文是纯粹加载的。 任何对可能出错的人提出建议的人?

非常感谢你。 /的Morten

编辑:这是完整的堆栈跟踪:

org.dbunit.dataset.NoSuchTableException: newsletter
    at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:288)
    at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:109)
    at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
    at com.github.springtestdbunit.DbUnitRunner.setupOrTeardown(DbUnitRunner.java:162)
    at com.github.springtestdbunit.DbUnitRunner.beforeTestMethod(DbUnitRunner.java:60)
    at com.github.springtestdbunit.DbUnitTestExecutionListener.beforeTestMethod(DbUnitTestExecutionListener.java:160)
    at com.github.springtestdbunit.TestExecutionListenerChain$3.call(TestExecutionListenerChain.java:93)
    at com.github.springtestdbunit.TestExecutionListenerChain.runChain(TestExecutionListenerChain.java:126)
    at com.github.springtestdbunit.TestExecutionListenerChain.forwards(TestExecutionListenerChain.java:115)
    at com.github.springtestdbunit.TestExecutionListenerChain.beforeTestMethod(TestExecutionListenerChain.java:91)
    at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:348)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    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.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    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:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)


Process finished with exit code -1

这是JobAdsNewsletter类:

@Entity
@Table(name = "newsletter", catalog = "watch_contentapi")
@Cache(usage= CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class JobAdNewsletter implements BaseObject<Integer>, java.io.Serializable {
    private Integer id;
    private Calendar timestamp;
    private String jobAdIds; //This is a string of all the jobads ids sent     out at that time.

    public JobAdNewsletter() {}

    public JobAdNewsletter(Integer id, Calendar timestamp, String jobAdIds){
        this.id = id;
        this.timestamp = timestamp;
        this.jobAdIds = jobAdIds;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {    return id;}
    public void setId(Integer id) {this.id = id;}

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "timestamp", nullable = false)
    public Calendar getTimestamp() { return timestamp; }
    public void setTimestamp(Calendar date) {
        this.timestamp = date;
    }

    @Column(name = "jobAdIds")
    public String getJobAdIds() {return jobAdIds;}
    public void setJobAdIds(String jobAdIds) {
        this.jobAdIds=jobAdIds;
    }
}

0 个答案:

没有答案