The following are the typical steps you need to do in your test case class. Remember, a typical test method does its test through the following steps:
- Set up the object into the desired state
- Execute the method on the tested object
- Execute the assertions to verify the state of the object
Register Schema
In order for DbFixture to set up the database automatically upon the first database connection,
you need to register the schema to the class DatabaseRegistry.
static {
DatabaseRegistry.instance().registerSchema(new Schema() {
public String getDbName() {
return databaseName();
}
public void setupSchema(HsqlDb database) {
database.execute("create table person (\n" +
" person_key integer IDENTITY,\n" +
" ssn varchar(10) not null,\n" +
" first_name varchar(64) null,\n" +
" last_name varchar(64) null,\n" +
" birthdate datetime null,\n" +
" CONSTRAINT person_primary_key PRIMARY KEY (person_key)\n" +
")\n" +
";\n" +
"create index person_ssn on person (ssn)\n" +
";");
}
});
}
Set Up Data
Set up the object to be tested by passing in the JDBC URL for the DbFixture proxy driver.
private String databaseName() {
return "tutorial";
}
public void testApi() {
String jdbcUrl = ProxyDriver.jdbcUrl(databaseName());
ProductionCode testedObject = new ProductionCode(jdbcUrl, "user", "password");
}
Execute the method
Execute the method under test
testedObject.insertSomeData("383-23-2888", "John", "Smith");
Execute the assertions
Use the Table to verify that the tested code did insert data into the database.
ResultRow row = personDb().table("person").row("ssn='383-23-2888'");
assertEquals("John", row.column("first_name"));
assertEquals("Smith", row.column("last_name"));
Files
Diagram
In a nutshell, the following is the picture.
