Java Reference
In-Depth Information
@Test
public void testAddUser() throws Exception {
User user = newUser();
long id = dao.addUser(user);
[...]
}
}
[...]
public final class EntitiesHelper {
C
D
E
public static final String USER_FIRST_NAME = "Jeffrey";
public static final String USER_LAST_NAME = "Lebowsky";
public static final String USER_USERNAME = "ElDuderino";
F
public static User newUser() {
User user = new User();
user.setFirstName(USER_FIRST_NAME);
user.setLastName(USER_LAST_NAME);
user.setUsername(USER_USERNAME);
return user;
}
public static void assertUser(User user) {
assertNotNull(user);
assertEquals(USER_FIRST_NAME, user.getFirstName());
assertEquals(USER_LAST_NAME, user.getLastName());
assertEquals(USER_USERNAME, user.getUsername());
}
The newUser() method is statically imported B and called C in the helper class F .
The helper class itself D provides an assertUser() method G (used by testGet-
UserById() ); it uses constants E to define the User attributes.
G
17.4.1
Filtering data sets
The method IDatatabaseConnection.createDataSet() returns a dataset represent-
ing the whole database. This is fine in our example, where the database has only one
table and the database access is fast (because it's an embedded database). In most
other cases, though, it would be overkill—either the test would fail because it would
return tables that we're not interested in or it would be slow to run.
The simplest way to narrow the field is by filtering the tables returned by create-
DataSet() , by passing an array containing the name of the tables that should be
returned. Applying that change to the previous example, we have the following:
IDataSet actualDataSet = dbunitConnection.createDataSet(
new String[] { "users" } );
A similar approach is to use a FilteredDataSet to wrap the dataset containing the
full database:
IDataSet actualDataSet = dbunitConnection.createDataSet();
FilteredDataSet filteredDataSet = new FilteredDataSet(
new String[] {"users"}, actualDataSet );
 
 
Search WWH ::




Custom Search