Java Reference
In-Depth Information
can't be obtained from EntityManager or EntityManagerFactory , the objects our test
case has references to. To overcome this limitation, we need our first hack: using an
Ant task ( JPAConfigurationTask ), which is also part of Hibernate Tools.
This is where the schema is effectively generated. The problem is, SchemaExport
either exports it to the system output or to a file. Ideally, it should allow the schema to
be exported to a Writer or OutputStream object, so we could pass a StringWriter as
a parameter to this method. Because this isn't the case, we have to use a second hack:
replace the System.out when this method is executed and then restore it afterwards.
Yes, we know this is ugly, but we warned you.
A final hack: because JPAConfigurationTask 's createConfiguration() method is
protected, our test case won't have access to it, so we create a subclass and override
that method, making it accessible to classes in the same package.
D
E
18.7
Summary
Using ORM tools (such as Hibernate and JPA ) greatly simplifies the development of
database access code in Java applications. But regardless of how great the technology
is or how much it automates the work, it's still necessary to write test cases for code
that uses it.
A common practice among Java EE applications is to use Façades and DAO s in the
business and persistence layers (respectively). In this chapter, we showed how to test
these layers in isolation, using mocks as DAO implementation.
We also showed how to leverage DbUnit and advanced techniques to effectively
test the JPA -based persistence layer, first testing the entity mappings and then the
DAO s, properly speaking. We also demonstrated how to use JPA -generated ID s in your
DbUnit datasets.
And although JPA testing is an extension of database testing (which was covered in
chapter 17), it has its caveats, such as lazy initialization exceptions, duplicated objects
returned in queries, cascade deletes, and generation of weird constraint names. The
examples in this chapter demonstrated how to deal with such issues.
 
Search WWH ::




Custom Search