Java Reference
In-Depth Information
as Arquillian. Along the way we highlighted some best practices to make your code testing
most effective. We'll review these best practices next.
15.5. Testing effectively
To test your code effectively, you'll need multiple levels of testing. Each level is focused
on testing a specific aspect of your code. As long as you keep the tests at a particular level
focused, you'll have an effective testing strategy.
The first level of testing is unit testing. The focus of unit testing is to verify that all of the
business logic of all of your classes is working as expected. The strategy for doing this is
to write tests to focus only on one class, mock whatever dependencies the class has, and
vary both the data input to the class and the data returned by the mock to cover all possible
business rules. Assert results returned from the class being tested against expected results.
Whenever you mock a class that's also part of your application, you need to make sure you
provide unit tests for that class as well so all of the classes in your application are covered.
Unit tests shouldn't require a lot of setup and should be run quickly so broken business
rules will be discovered immediately. Unit tests shouldn't rely on any external resources
(database, web service, naming directory, and so on) to execute; all interaction with extern-
al services like this should be mocked returning appropriate data for the unit test. Keeping
unit testing focused like this can result in a 100% covered green bar suite of tests on code
that's completely undeployable to any Java EE server. But that's OK, because that isn't
the focus of unit testing. Getting your code deployed is the focus of the next level of test-
ing—integration testing.
The focus of integration testing is to verify that all of the different technologies used in
the application can actually come together and function properly as a whole. Integration
testing answers questions like these: Does the data persist to the database? Will the EJB
be deployed? Will the interceptors fire in the order I expect them? During unit testing you
verify that individual classes are functioning properly on their own. Now during integra-
tion testing you verify that the individual classes can come together (inside a container) as
a whole and function as you expect them to. To perform integration testing, the Java EE
specification defines the EJBContainer , which is an embedded container that can be
started in-memory by any Java SE code. The EJBContainer may be used directly or
it may be used indirectly by an integration testing tool that wraps it, such as Arquillian.
In either case, the project configuration and configuration of all the different technologies
Search WWH ::




Custom Search