Dashboard > DbFixture > Home > One Minute Tutorial
One Minute Tutorial Log In View a printable version of the current page.

Added by Shane Duan , last edited by Shane Duan on Feb 09, 2006  (view change)
Labels: 
(None)

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:

  1. Set up the object into the desired state
  2. Execute the method on the tested object
  3. 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 {
  // static block so that the schema gets registered only once
    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() {
    // JDBC URL: jdbc:dbfixture:proxy:<database-name>
    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.

Powered by a free Atlassian Confluence Open Source Project / Non-profit License granted to ThoughtWorks, Inc.. Evaluate Confluence today.
Powered by Atlassian Confluence 2.7.1, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators