Java Reference
In-Depth Information
Some table or column name (like user, role, or group) might be a reserved
word in the chosen database.
If you don't set the relationship annotations (like @OneToMany ) correctly, JPA
will create many weird mappings, sometimes even unnecessary tables.
When dependent objects must be persisted automatically, it's important to ver-
ify that the proper cascade options have been set.
Not to mention the mother of all reasons why you write unit tests: you need to some-
how verify that JPA is persisting/restoring your objects! Without the entity mapping
tests, you'd have to either rely on your IDE or manually test it using the main()
method of some hacky class.
In order to avoid these issues, it's a good practice to write a couple (at least two:
one for loading, another for saving) of unit tests for each primary persistent entity in
the system. You don't need to write tests for all of them, though. Some entities are too
simple or can be indirectly tested (like the Telephone entity in our examples).
Also, because the load and save tests are orthogonal, they should use the same
dataset whenever possible, to facilitate maintenance; listing 18.9 shows the dataset
used in the user entity mapping tests (listing 18.10).
Listing 18.9
Dataset file for User and Telephone tests, user-with-telephone.xml
<?xml version="1.0"?>
<dataset>
<users id="${id}" username="ElDuderino"
first_name="Jeffrey" last_name="Lebowsky" />
<phones id="${phoneId}" user_id="${id}" type="0" number="481 516-2342"/>
<phones id="${phoneId+1}" user_id="${id}" type="2"
number="108 555-6666"/>
</dataset>
Notice that the ID s are dynamically defined using EL expressions, so the test cases suc-
ceed regardless of the order in which they're executed.
Listing 18.10
Entity mapping unit tests for User and Telephone classes
[...]
public class EntitiesMappingTest extends AbstractJpaDbUnitELTemplateTestCase
{
@Test
@DataSets(setUpDataSet="/user-with-telephone.xml")
public void testLoadUserWithTelephone() {
beginTransaction();
User user = em.find(User. class , id);
commitTransaction();
assertUserWithTelephone(user);
id++; phoneId+=2;
}
B
C
D
E
F
@Test
@DataSets(assertDataSet="/user-with-telephone.xml")
 
 
 
Search WWH ::




Custom Search