Created
December 18, 2012 17:28
-
-
Save matheustardivo/4329999 to your computer and use it in GitHub Desktop.
Testing with dbunit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.walmart.customer.services.frontend.domain.test; | |
import java.sql.Connection; | |
import javax.sql.DataSource; | |
import org.dbunit.database.DatabaseConnection; | |
import org.dbunit.database.IDatabaseConnection; | |
import org.dbunit.dataset.IDataSet; | |
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; | |
import org.dbunit.operation.DatabaseOperation; | |
import org.junit.Before; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.jdbc.datasource.DataSourceUtils; | |
public class DatabaseIntegration { | |
private String filename; | |
@Autowired | |
public DataSource dataSource; | |
public DatabaseIntegration(String filename) { | |
this.filename = filename; | |
} | |
@Before | |
public void setupDB() throws Exception { | |
Connection connection = DataSourceUtils.getConnection(dataSource); | |
IDatabaseConnection dbUnitConnection = new DatabaseConnection( | |
connection); | |
FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder(); | |
builder.setColumnSensing(true); | |
IDataSet dataSet = builder.build(getClass().getClassLoader() | |
.getResourceAsStream(filename)); | |
try { | |
DatabaseOperation.REFRESH.execute(dbUnitConnection, dataSet); | |
} finally { | |
DataSourceUtils.releaseConnection(connection, dataSource); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.walmart.customer.services.frontend.domain; | |
import java.util.List; | |
import org.junit.Assert; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.test.context.ActiveProfiles; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import org.springframework.transaction.annotation.Transactional; | |
import com.walmart.customer.services.frontend.domain.test.DatabaseIntegration; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(locations = "classpath:spring-context.xml") | |
@Transactional | |
@ActiveProfiles("test") | |
public class DummyTest extends DatabaseIntegration { | |
@Autowired | |
private DummyDAO dummyDAO; | |
public DummyTest() { | |
super("dbunit/DummyTest.xml"); | |
} | |
@Test | |
public void findAll() { | |
List<Dummy> list = dummyDAO.findAll(); | |
Assert.assertNotNull(list); | |
Assert.assertEquals(4, list.size()); | |
Assert.assertEquals("Name 1", list.iterator().next().getName()); | |
} | |
@Test | |
public void persist() { | |
Dummy dummy = new Dummy(); | |
dummy.setName("New dummy"); | |
dummyDAO.persist(dummy); | |
Assert.assertNotNull(dummy.getId()); | |
Assert.assertTrue(dummy.getId() > 0); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | |
<dataset> | |
<Dummy id="1" name="Name 1" /> | |
<Dummy id="2" name="Name 2" /> | |
<Dummy id="3" name="Name 3" /> | |
<Dummy id="4" name="Name 4" /> | |
</dataset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>org.dbunit</groupId> | |
<artifactId>dbunit</artifactId> | |
<version>2.4.8</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>com.h2database</groupId> | |
<artifactId>h2</artifactId> | |
<version>1.3.168</version> | |
</dependency> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<beans profile="test"> | |
<jdbc:embedded-database id="dataSource" type="H2" /> | |
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> | |
<property name="persistenceUnitName" value="persistenceUnit"/> | |
<property name="dataSource" ref="dataSource" /> | |
<property name="jpaVendorAdapter"> | |
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> | |
<property name="database" value="H2" /> | |
<property name="showSql" value="true" /> | |
</bean> | |
</property> | |
<property name="jpaProperties"> | |
<props> | |
<prop key="hibernate.hbm2ddl.auto">create-drop</prop> | |
</props> | |
</property> | |
</bean> | |
</beans> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment