Rate this page:
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:
Testing dependencies
1 2 3 4 5 6 7 8 9 10 11 12
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.java.dev.activeobjects</groupId>
<artifactId>activeobjects-test</artifactId>
<version>${ao.version}</version>
<scope>test</scope>
</dependency>
Tests of Active Objects entities look like the following, taken from the DogFood blog:
Testing Active Objects
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
@RunWith(ActiveObjectsJUnitRunner.class) // (1)
@Data(AoBlogServiceTest.AoBlogServiceTestDatabaseUpdater.class) // (2)
@Jdbc(DerbyEmbedded.class) // (3)
@NameConverters // (4)
public final class AoBlogServiceTest
{
private EntityManager entityManager; // (5)
private AoBlogService blogService;
@Before
public void setUp() throws Exception
{
blogService = new AoBlogService(entityManager);
}
@Test
public void getBlogCreatesOneIfNecessary() throws Exception // (6)
{
assertEquals(0, entityManager.find(Blog.class).length); // no blogs
assertNotNull(blogService.getBlog());
}
public static final class AoBlogServiceTestDatabaseUpdater implements DatabaseUpdater // (2)
{
@Override
public void update(EntityManager entityManager) throws Exception
{
entityManager.migrate(Blog.class, Post.class, Comment.class, Label.class, PostToLabel.class);
}
}
}
ActiveObjectsJUnitRunner
makes testing ActiveObjects entities easy.DatabaseUpdater
through the @Data
annotation. 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 DatabaseUpdater
).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.@NameConverters
annotation allows setting the converter used for tests. Here the defaults are used. Note that the annotation can be omitted.EntityManager
is injected by the ActiveObjectsJUnitRunner
, which allows instantiating the class under test with a properly configured entity manager.Rate this page: