Active Objects integrates with JUnit to make your entity testing easy.
The first thing to do is add a dependency to your project. When using Maven, this means:
Tests of Active Objects entities look like the following, taken from the DogFood blog:
- Using the
ActiveObjectsJUnitRunnermakes testing ActiveObjects entities easy.
- Specifying a
@Dataannotation. It is implemented here as a static inner class (
AoBlogServiceTestDatabaseUpdater). Its update method allows you to prepare the database for tests. It is called only once per class (or per set of classes with the same
- Specifying the database to use for testing, here an embedded Derby database. A convenient class to re-use for Maven users is the
net.java.ao.test.jdbc.DynamicJdbcConfiguration, which will determine the database in use according to a System property. Combined with Maven profiles, it makes it easy to test against any database.
- Active Objects uses name converters to translate entity names and properties to table and column names. This
@NameConvertersannotation allows setting the converter used for tests. Here the defaults are used. Note that the annotation can be omitted.
EntityManageris injected by the
ActiveObjectsJUnitRunner, which allows instantiating the class under test with a properly configured entity manager.
- A simple test. Note that all test methods are wrapped in a transaction that is rolled back after the method has been run. This will leave the database in the exact same state before each test, whatever happens within the individual test.
Was this page helpful?