title: Executing Initialization Code only once for multiple test classes
body: <p>I am writing unit test cases for my code. I am using PowerMockito with Junit. I have written an initialization code which will take care of all the initialization stuff in my application. Below is the way my code is structured:</p>
  
  <pre><code>Class ServiceInitializer{
      public static isInitialized = Boolean.FALSE;
  
      public static void initialize(){
          //Initilization Code Goes Here
          isInitialized = Boolean.TRUE;
      }
  }
  
  @RunWith(PowerMockRunner.class)
  class BaseTest{
  
      @Before
      public void preTest(){
          //some code
      }
  
  
      static{
          if(!ServiceInitializer.isInitialized){
              ServiceInitializer.initialize();
          }
      }
  
      @After
      public void postTest(){
          //some code
      }
  }
  
  public TestClass1 extends BaseTest{
      @Test
      public void testMethodA_1(){//test code}
  
      @Test
      public void testMethodA_2(){//test code}
  
      @Test
      public void testMethodA_3(){//test code}
  }
  
  public TestClass2 extends BaseTest{
      @Test
      public void testMethodB_1(){//test code}
  
      @Test
      public void testMethodB_2(){//test code}
  
      @Test
      public void testMethodB_3(){//test code}
  }
  </code></pre>
  
  <p>I am executing these test cases as ant script using Junit and batchtest targets as given below:</p>
  
  <pre><code>&lt;junit printsummary="yes" haltonfailure="yes" showoutput="yes"&gt;
              &lt;classpath refid="JUnitTesting.classpath"/&gt;
              &lt;batchtest haltonerror="false" haltonfailure="no" todir="${junit.output.dir}/raw"&gt;
                  &lt;formatter type="plain"/&gt;
                  &lt;formatter type="xml"/&gt;     
                  &lt;fileset dir="${basedir}/src"&gt;
                    &lt;include name="**/Test*.java"/&gt;
                  &lt;/fileset&gt;
              &lt;/batchtest&gt;
          &lt;/junit&gt;
  </code></pre>
  
  <p>I am able to execute the test cases just fine but its the initialization code, which is written in the static block of BaseTest class, which is causing problem for me. Whenever <strong>New Test Class</strong> starts its execution,  "ServiceInitializer.initialize();" is getting called every time. So, say if I have 10 Test Classes then this method is getting called 10 times. I want to get control over it such that it only gets executed only once no matter how many Test Classes I have. How Can I achieve that? Is that even possible with JUnit?</p>
  
  <p><strong>--Update--</strong></p>
  
  <p>I know there is <code>@BeforeClass</code> annotation available which executes the block of code exactly once for each test class. But this is not fulfilling my requirements since as soon as JUnit runs the other test class it calls the method under "@BeforeClass" annotation and runs the initialization code again in spite of the initialization check there.</p>