-1

I read some of the documentation of DbUnit and i can not understand why i must set the current data base url "jdbc:mysql://127.0.0.1/hr", "hr", "hr"); and after that i set up a "fake" data base in hr-seed.xml. I know that dbunit uses for data base only the xml file but for what is the setup connection to the real data base.

 protected IDatabaseConnection getConnection() 
      throws Exception {

       Class driverClass = 
         Class.forName("org.gjt.mm.mysql.Driver");

       Connection jdbcConnection = 
         DriverManager.getConnection(
          "jdbc:mysql://127.0.0.1/hr", "hr", "hr");

       return new DatabaseConnection(jdbcConnection);
    }

    protected IDataSet getDataSet() throws Exception {
       return new FlatXmlDataSet(
          new FileInputStream("hr-seed.xml"));
    }
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <EMPLOYEE employee_uid='1' 
            start_date='2001-01-01'         
            first_name='Drew' ssn='333-29-9999' 
            last_name='Smith' />
  <EMPLOYEE employee_uid='2' 
            start_date='2002-04-04'         
            first_name='Nick' ssn='222-90-1111' 
            last_name='Marquiss' />
  <EMPLOYEE employee_uid='3' 
            start_date='2003-06-03'         
            first_name='Jose' ssn='111-67-2222' 
            last_name='Whitson' />
</dataset>
rocker
  • 69
  • 1
  • 6

2 Answers2

1

DbUnit can work with real database. You can do it only with xml.

First add to pom.xml smth like this (version can be another):

<dependency>
    <groupId>org.dbunit</groupId>
    <artifactId>dbunit</artifactId>
    <version>2.5.0</version>
</dependency>

<dependency>
    <groupId>com.github.springtestdbunit</groupId>
    <artifactId>spring-test-dbunit</artifactId>
    <version>1.1.0</version>
</dependency>

Then add to your test resources directory spring-config.xml (I use postgresql)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">


    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="username" value="****" />
        <property name="password" value="*****" />
        <property name="url" value="url-to-server-with-your-db"/>
    </bean>

    <bean id="jdbcTemplate"
          class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="dataSource" />
    </bean>

</beans>

Add beans for classes you need to test in this xml.

In test class add annotations:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring-config.xml"})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class,
        DbUnitTestExecutionListener.class})

Before test method add annotations you needed depending on the tests goal. For example:

@DatabaseSetup(value = "/testData.xml")
@DatabaseTearDown(value = "/testData.xml")

What does it mean? You have your xml with dataset. Data in this file will push to your database before test (@DatabaseSetup), and after test you can do this again (@DatabaseTearDown) - the same file for restore the initial state or another file as you need. That's all.

Optio
  • 7,244
  • 2
  • 22
  • 30
0

Try MariaDB4j https://github.com/vorburger/MariaDB4j . Then connect to a in-memory mysql to test. It change my life already.

MariaDB4j is a Java "launcher" for MariaDB (=MySQL(R)), allowing to use it from Java without ANY installation / external dependencies. Read again: You do NOT have to have MariaDB binaries installed on your system to use MariaDB4j!

wuranbo
  • 101
  • 7