title: IntelliJ running tests one by one
body: <p>To be short - I' just wrote a singleton class. Like the all singletons consists my singleton of a private constructor and a static field containing a predefined amount of objects of the class.</p>
  
  <p>The case is - the singleton class has to be initialized using a JSON file and the expected behavior is it throwing an exception when no files from a given list are present. </p>
  
  <p>The problem is that apparently IntelliJ (or maybe just JUnit) is loading all test classes and then executing them one by one in one program in a random order. This means that by the time the class which test the wrong initialization has been reached, the singleton already has been initialized. This means that every time I run all the tests at once I'll have one test which did not pass and when I run them one by one manually it'll result in all the tests passing.</p>
  
  <p>Is there a way to force IntelliJ to run all the test classes separated?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I don't know how this could change anything, but lets assume it will. Here is a sample code with the exact same behavior. It's an IDE question, not a java speciffic one.</p>
  
  <pre><code>public class Singleton {
      private static Singleton singleton = new Singleton();
  
      public static Singleton getInstance() {
          return singleton;
      }
  
      private Singleton() {
          if (!new File("maybeIExist.txt").exists())
              throw new ExceptionInInitializerError();
      }
  
      public void doStuff() {
          System.out.println("stuff");
      }
  }
  </code></pre>
  
  <p>And of course the tests:</p>
  
  <pre><code>public class SingletonTestOne {
      @Test(expected = ExceptionInInitializerError.class)
      public void test() throws Exception {
          Singleton.getInstance();
      }
  }
  </code></pre>
  
  <p>And the second one:</p>
  
  <pre><code>public class SingletonTestTwo {
      @Before
      public void before() throws Exception {
          //code creating file
      }
      @Test
      public void test() throws Exception {
          Singleton.getInstance();
          doStuff();
      }
      @After
      public void after() throws Exception {
          //code deleting file
      }
  }
  </code></pre>