Java Reference
In-Depth Information
assertTrue(id>0);
}
@Test
@DataSets(setUpDataSet="/user.xml",assertDataSet="/empty.xml")
public void
testDeleteUser()
throws
Exception {
beginTransaction();
long
id = ELFunctionMapperImpl.getId(User.
class
);
dao.deleteUser(id);
commitTransaction();
}
}
Compare this test case with chapter 17's latest version (listing 17.24) of the equivalent
test; they're almost the same, the only differences being the pretest method
prepare-
Dao()
(which instantiates a
DAO
object and sets its
EntityManager
), the local variable
representing the user's
ID
(because of the Hibernate
ID
generation integration we dis-
cussed in the previous section), and the transaction management calls.
But now the
User
object could also have a list of telephones; it's necessary, then, to
add analogous test cases to handle this scenario, as shown in listing 18.19. Note that if
the
User
/
Telephone
relationship was mandatory (and not optional), the new tests
would replace the old ones (instead of being added to the test class).
Listing 18.19
New test cases on
UserDaoJpaImplTest
to handle user with telephone
[...]
public class
UserDaoJpaImplTest
extends
AbstractJpaDbUnitELTemplateTestCase {
[...]
@Test
@DataSets(setUpDataSet="/user-with-telephone.xml")
public void
testGetUserByIdWithTelephone() throws Exception {
beginTransaction();
long
id = ELFunctionMapperImpl.getId(User.
class
);
User user = dao.getUserById(id);
commitTransaction();
assertUserWithTelephone(user);
}
@Test
@DataSets(assertDataSet="/user-with-telephone.xml")
public void
testAddUserWithTelephone()
throws
Exception {
beginTransaction();
User user = newUserWithTelephone();
dao.addUser(user);
commitTransaction();
long
id = user.getId();
assertTrue(id>0);
}
@Test
@DataSets(setUpDataSet="/user-with-telephone.xml",
assertDataSet="/empty.xml")