Java Reference
In-Depth Information
the classes that will use the annotation. Because it isn't possible to use null , we use the
empty string to indicate no dataset.)
Listing 17.21 UserDaoJdbcImplTest using custom annotations
[...]
public class UserDaoJdbcImplAnnotationTest extends
AbstractDbUnitTemplateTestCase {
@Test
@DataSets(setUpDataSet="/user-token.xml")
public void testGetUserById() throws Exception {
User user = dao.getUserById(id);
assertUser(user);
}
@Test
@DataSets(assertDataSet="/user-token.xml")
public void testAddUser() throws Exception {
User user = newUser();
id = dao.addUser(user);
assertTrue(id>0);
}
}
Compare this new test class with the previous example (listing 17.19). You barely
notice the template pattern being used.
The magic is done by the AbstractDbUnitTemplateTestCase , which extends
AbstractDbUnitTestCase and uses a custom TestRunner ; this TestRunner intercepts
the test methods 12 and plays the template role. Listing 17.22 shows this new superclass.
Listing 17.22
New superclass, AbstractDbUnitTemplateTestCase
[...]
@RunWith(AbstractDbUnitTemplateTestCase.DataSetsTemplateRunner. class )
public abstract class AbstractDbUnitTemplateTestCase extends
AbstractDbUnitTestCase {
B
C
protected static long id;
public static class DataSetsTemplateRunner extends JUnit4ClassRunner {
public DataSetsTemplateRunner(Class<?> klass) throws
InitializationError {
super (klass);
}
@Override
protected void invokeTestMethod(Method method,
RunNotifier notifier) {
setupDataSet(method);
super.invokeTestMethod(method, notifier);
assertDataSet(method);
}
D
12
This technique is explained in more detail in appendix B.
 
 
Search WWH ::




Custom Search