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