Uploaded image for project: 'XWiki Commons'
  1. XWiki Commons
  2. XCOMMONS-373

JUnit4 Runner for running tests with some classes in isolation

    XMLWordPrintable

Details

    • N/A
    • N/A

    Description

      Provide a specialized JUnit4 runner to isolate some classes during the execution of a test class. This runner should be used in combination with the a new @IsolatedClassPrefix() annotation.

      Isolation is helpful when you do not want to pollute the application ClassLoader with some classes under test, or some dynamically loaded classes during a test. It could be used to reinitialize statics and drop out those dynamically loaded classes after the test.

      To use it, define a JUnit @RunWith annotation on your test class and also add a IsolatedClassPrefix() annotation to define the list of class prefixes that should be isolated from the rest of your tests.

      For example:

      @RunWith(IsolatedTestRunner)
      @IsolatedClassPrefix("org.xwiki.mypackage")
      public class MyPackageTest
      {
          @Test
          public void someTest() throws Exception
          {
          ...
      }
      

      The prefixes should at least include a prefix that match your test class, else the initialization will fail since your test would not be run in isolation properly.

      If you are mocking some of your isolated classes with Mockito in different tests (either isolated or not), you will need to disable the class cache used by Mockito to avoid ClassCastException during mocking. You can disable the cache by adding the following class to your test Jar :

      package org.mockito.configuration;
      public class MockitoConfiguration extends DefaultMockitoConfiguration
      {
          @Override
          public boolean enableClassCache()
          {
              return false;
          }
      }
      

      Attachments

        Issue Links

          Activity

            People

              softec Denis Gervalle
              softec Denis Gervalle
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: