Created
February 22, 2012 19:51
-
-
Save marcphilipp/1886871 to your computer and use it in GitHub Desktop.
Database Tests 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 de.marcphilipp.dbunit.example; | |
import static org.h2.engine.Constants.UTF8; | |
import static org.hamcrest.CoreMatchers.is; | |
import static org.hamcrest.CoreMatchers.nullValue; | |
import static org.junit.Assert.assertThat; | |
import java.io.File; | |
import javax.sql.DataSource; | |
import org.dbunit.IDatabaseTester; | |
import org.dbunit.JdbcDatabaseTester; | |
import org.dbunit.dataset.IDataSet; | |
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; | |
import org.dbunit.operation.DatabaseOperation; | |
import org.h2.jdbcx.JdbcDataSource; | |
import org.h2.tools.RunScript; | |
import org.junit.Before; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
public class XmlDatabaseTest { | |
private static final String JDBC_DRIVER = org.h2.Driver.class.getName(); | |
private static final String JDBC_URL = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"; | |
private static final String USER = "sa"; | |
private static final String PASSWORD = ""; | |
@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("dataset.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(); | |
} | |
@Test | |
public void findsAndReadsExistingPersonByFirstName() throws Exception { | |
PersonRepository repository = new PersonRepository(dataSource()); | |
Person charlie = repository.findPersonByFirstName("Charlie"); | |
assertThat(charlie.getFirstName(), is("Charlie")); | |
assertThat(charlie.getLastName(), is("Brown")); | |
assertThat(charlie.getAge(), is(42)); | |
} | |
@Test | |
public void returnsNullWhenPersonCannotBeFoundByFirstName() throws Exception { | |
PersonRepository repository = new PersonRepository(dataSource()); | |
Person person = repository.findPersonByFirstName("iDoNotExist"); | |
assertThat(person, is(nullValue())); | |
} | |
private DataSource dataSource() { | |
JdbcDataSource dataSource = new JdbcDataSource(); | |
dataSource.setURL(JDBC_URL); | |
dataSource.setUser(USER); | |
dataSource.setPassword(PASSWORD); | |
return dataSource; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment