Last active
August 29, 2015 14:03
-
-
Save up1/d7590386b48ea076dfca to your computer and use it in GitHub Desktop.
Demo :: 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
private DataSource getDataSource() { | |
JdbcDataSource dataSource = new JdbcDataSource(); | |
dataSource.setURL(JDBC_URL); | |
dataSource.setUser(USER); | |
dataSource.setPassword(PASSWORD); | |
return 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
<dataset> | |
<PERSON FIRSTNAME="Up1" AGE="18"/> | |
<PERSON FIRSTNAME="Up2" AGE="20"/> | |
</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
@Test | |
public void findAndReadExistingPersonByUserID() { | |
PersonDAO personDAO = new PersonDAO(getDataSource()); | |
Person person = personDAO.getPerson(1); | |
assertEquals(1, person.getID()); | |
assertEquals("Up1", person.getFirstName()); | |
} |
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
@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(); | |
} |
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
<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> |
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
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