Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Test DBUnit and table schema name

Is it possible to set the table schema name when using @DatabaseSetup annotation from Spring Test DBUnit? Currently I'm using it like this:

@DatabaseSetup("user-data.xml")
public class UserMapperTest {
}

user-data.xml: (I also tried to set the element name to user.system_user without any luck)

<dataset>
    <system_user
        ...
        />
</dataset>

And here I'm creating my table with schema called user:

create table "user".system_user (...);

And this is the exception that I get when running test:

org.h2.jdbc.JdbcSQLException: Table "SYSTEM_USER" not found; SQL statement:
delete from SYSTEM_USER [42102-175]
like image 909
perak Avatar asked Apr 01 '14 20:04

perak


3 Answers

I used this trick. First, we need OracleConnection bean:

<bean id="oracleConnection" class="org.dbunit.ext.oracle.OracleConnection">
    <constructor-arg value="#{dataSource.getConnection()}"/>
    <constructor-arg value="<your_scheme_name>"/>
</bean>

Then you can use this annotation in your methods

@DbUnitConfiguration(databaseConnection = "oracleConnection")
@DatabaseSetup(...)

Hope it helps.

like image 199
int21h Avatar answered Oct 31 '22 03:10

int21h


If you are using the spring-test-dbunit then you need to create an IDatabaseConnection with a specific DBUnit configuration. Following the doc's example I've setup this one for me:

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

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
">
    <bean id="dbUnitDatabaseConfig" class="com.github.springtestdbunit.bean.DatabaseConfigBean">
        <property name="datatypeFactory">
            <bean class="org.dbunit.ext.postgresql.PostgresqlDataTypeFactory" />
        </property>
        <!--property name="qualifiedTableNames" value="true" /-->
        <property name="caseSensitiveTableNames" value="true" />
    </bean>

    <bean id="dbUnitDatabaseConnection" class="com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="databaseConfig" ref="dbUnitDatabaseConfig"/>
        <property name="schema" value="sapeo"/>
    </bean>

</beans>
like image 29
Gilberto Avatar answered Oct 31 '22 03:10

Gilberto


I had a similar issue. You cannot with the annotation however you can specify the schema on the connection string. Here is how I solved my case:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:mem:fullywallet;MVCC=true" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <list>
            <value>your.package.to.be.scanned</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.default_schema">public</prop>
        </props>
    </property>
</bean>

In my case I was using HSQLDB but it is applicable for other databases as well

like image 1
Paizo Avatar answered Oct 31 '22 03:10

Paizo