While migrating from JUnit 3.x to the new 4.x framework, one method I found that was missing was Spring‘s getLoadCount() method on the
AbstractDepenpendencyInjectionTest. The value returned allowed us to tell whether a test was running by itself or in a group a tests (like with the ant junit-report target or right-clicking on /test/src and choosing run-as junit test).

Having this method was useful for two reasons:

  1. A test like CreateInitialDataSet that is meant to be configured and run alone.  If this test sees that its being run in a group, it returns early and doesn’t actually run the test
  2. The module tests needed to have the application context refreshed before running.  To do this, I made the previous test mark the context as “dirty” so that the test directly following it would have a fresh app context.

There were actually two separate solutions:

  1. All tests extend BaseContextSensitiveTest for convenience methods and setup.  Every junit class will invoke this class and its constructor.  By adding a static variable loadCount and incrementing that in the constructor, I am able to know how many tests have been run up to that point.
  2. The application context doesn’t need to be marked as dirty.  The dummy method could actually be removed without replacement.

This is in the openmrs category tagged as , ,

Add a comment »

Leave a Comment

Your comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.