Skip to content

Instantly share code, notes, and snippets.

@up1
Last active August 29, 2015 14:03
Show Gist options
  • Save up1/d7590386b48ea076dfca to your computer and use it in GitHub Desktop.
Save up1/d7590386b48ea076dfca to your computer and use it in GitHub Desktop.
Demo :: DBUnit
private DataSource getDataSource() {
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setURL(JDBC_URL);
dataSource.setUser(USER);
dataSource.setPassword(PASSWORD);
return dataSource;
}
<dataset>
<PERSON FIRSTNAME="Up1" AGE="18"/>
<PERSON FIRSTNAME="Up2" AGE="20"/>
</dataset>
@Test
public void findAndReadExistingPersonByUserID() {
PersonDAO personDAO = new PersonDAO(getDataSource());
Person person = personDAO.getPerson(1);
assertEquals(1, person.getID());
assertEquals("Up1", person.getFirstName());
}
@BeforeClass
public static void createSchema() throws Exception {
RunScript.execute(JDBC_URL, USER, PASSWORD, "schema.sql", UTF8, false);
}
@Before
public void importDataSet() throws Exception {
IDataSet dataSet = readDataSet();
cleanlyInsert(dataSet);
}
private IDataSet readDataSet() throws Exception {
return new FlatXmlDataSetBuilder().build(new File("persions.xml"));
}
private void cleanlyInsert(IDataSet dataSet) throws Exception {
IDatabaseTester databaseTester = new JdbcDatabaseTester(JDBC_DRIVER,
JDBC_URL, USER, PASSWORD);
databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
databaseTester.setDataSet(dataSet);
databaseTester.onSetup();
}
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.167</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
<scope>test</scope>
</dependency>
</dependencies>
create table if not exists PERSON (
ID int identity primary key,
FIRSTNAME varchar,
AGE int
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment