Skip to content

Instantly share code, notes, and snippets.

@froop
Created January 17, 2012 22:38
Show Gist options
  • Save froop/1629470 to your computer and use it in GitHub Desktop.
Save froop/1629470 to your computer and use it in GitHub Desktop.
[Java][JUnit] DbUnit例
import org.dbunit.*;
public class DbUnitSampleTest extends DatabaseTestCase {
@Override
protected IDatabaseConnection getConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection jdbcConn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sample1", "user", "pass");
return new DatabaseConnection(jdbcConn);
}
@Override
protected IDataSet getDataSet() throws Exception {
return createDataSet(new File("test/dbunit", "prepare.xml"));
}
private IDataSet createDataSet(File file) throws Exception {
return new FlatXmlDataSetBuilder().build(file);
}
@Test
public void testInsert() throws Exception {
new DbUnitSample().insert();
IDataSet expected = createDataSet(new File("test/dbunit", "expected.xml"));
assertEqualsTable(expected, "sample1_1");
}
private void assertEqualsTable(IDataSet expectedDataSet, String tableName)
throws Exception {
ITable expectedTable = expectedDataSet.getTable(tableName);
IDataSet databaseDataSet = getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable(tableName);
Assertion.assertEquals(expectedTable, actualTable);
}
}
public class DtdCreate {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection jdbcConn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sample1", "user", "pass");
IDatabaseConnection connection = new DatabaseConnection(jdbcConn);
FlatDtdDataSet.write(connection.createDataSet(), new FileOutputStream(
"sample1.dtd"));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dataset SYSTEM "sample1.dtd">
<dataset>
<sample1_1 col1="1" col2="abc" />
<sample1_1 col1="2" col2="あいう" />
</dataset>
<!ELEMENT dataset (
sample1_1*,
sample1_2*)>
<!ELEMENT sample1_1 EMPTY>
<!ATTLIST sample1_1
col1 CDATA #REQUIRED
col2 CDATA #IMPLIED
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment