Thursday

Functional Junit Test - Part 2 - Making newly created session objects available for query

Making new session objects available for Object Level Query

Sometimes you may need to create data in the TopLink session to support your test scenarios. To make those newly created objects available for object level queries down the road in your execution patch, you need to do the following:
  1. register the new object into the session using UnitOfWork.registerNewObject()
  2. call alwaysConformResultsInUnitOfWork.
Then, the new object (though not committed to the database) will be seen by object level queries, e.g. when you call oracle.toplink.sessions.Session.readAllObjects(entityClass, expression), the new object, if satisfying the expression, will be included in the returned collection.

Remember that, if you read Part 1 of this series, the new object will not be saved into database. The changes are being rolled back for each test case as described in Part 1.

Making new session objects available for non-Object Level Query

If there are non-object level queries in your application, e.g. Named Query, and you want to create test data for the query, then do the following:


  1. register the new object into the session using UnitOfWork.registerNewObject()
  2. call UnitOfWork.writeChanges().
 There is a limitation: you can only call writeChanges() once per session.

1 comment:

Anonymous said...

If writechanges() can only used for once, how useful is it in continuous integration environment where bunch of tests are running? I would rather use other methods, e.g. DBunit or mock objects.