title: Java JUnit: Reset SSLContext in JUnit Test
body: <p>I'm writing some methods to manipulate the SSLContext. I wrote JUnit-Test to test the functionality. Unfortunately I'm stuck, because I can't find a way to reset the SSLContext. So I have to run every test on it's own within it's own JVM, because the affect of previous test will affect the next test. The SSLContext seams not to be resetable, because it is not written in a DI manner.</p>
  
  <p>Is there a way to reset the SSLContext? </p>
  
  <p>Is there a way to run every JUnit-Test in its own JVM? (I use Maven as Build-Management-Tool)</p>
  
  <p><s>This won't work</s> This will only partly work:</p>
  
  <pre><code>SSLContext defaultSSLContext = SSLContext.getDefault();
  
  // manipulate SSLContext
  
  SSLContext.setDefault(defaultSSLContext);
  </code></pre>
  
  <p><strong>Edit 2017-02-02</strong></p>
  
  <p><strong>My Java Installation was broken</strong></p>
  
  <p>My Java installtion was broken, so in my case the default SSLContext was null <code>(null == SSLContext.getDefault())</code>. So I could not set the SSLContext. The method raises a NullPointerException. I reinstalled Java and now the SSLContext is working again and the default SSLContext will not be null.</p>
  
  <p><strong>Reseting the SSLContext</strong></p>
  
  <p>Storing the SSLContext in an interim variable and restoring it afterwards will work for the SSLContext. But not for implementation like java.net.URL. These implementations seam to keep a Singleton instance of the SSLContext from the first call and will never change it during the runtime of the JVM.</p>
  
  <p><strong>JUnit forked Run with Maven</strong></p>
  
  <p>I followed <em>Stefan Birkner's</em> proposal and used Maven Surefire Plugin.</p>
  
  <p>I added in the project pom.xml:</p>
  
  <pre><code>&lt;/plugins&gt;
      [...]
      &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.19.1&lt;/version&gt;
          &lt;configuration&gt;
              &lt;reuseForks&gt;false&lt;/reuseForks&gt;
              &lt;forkCount&gt;2&lt;/forkCount&gt;
          &lt;/configuration&gt;
      &lt;/plugin&gt;
  &lt;/plugins&gt;
  </code></pre>
  
  <p>I split the JUnit Test into parts, that could run on their own without interfering. So <code>FooTest.java</code> became <code>Foo01Test.java ... Foo03Test.java</code>. Now the tests are running within Eclipse and with Maven.</p>