title: Jenkins build fails when closing Spring ApplicationContext
body: <p><strong>Why does running failing tests within a Spring application context cause my Jenkins build to crash?</strong> </p>
  
  <p>I am using Ant to build my application and run JUnit tests on Jenkins.</p>
  
  <p>I have not experienced any problems while running my Ant build within Eclipse, with or without failing tests. </p>
  
  <p>However, when I try to run the build with Jenkins, the build fails without recording an exception when it has finished running the JUnit tests:</p>
  
  <pre><code>...
  [junit] INFO: Executing Stored Procedure with parameters {@pivoting=1, @ignoreReference=0}
  [junit] Tests run: 7, Failures: 1, Errors: 0, Time elapsed: 7.831 sec
  [junit] Sep 22, 2015 4:23:57 PM org.springframework.context.support.AbstractApplicationContext doClose
  [junit] INFO: Closing org.springframework.context.support.GenericApplicationContext@8a590d1: 
      startup date [Tue Sep 22 16:23:52 BST 2015]; root of context hierarchy
  
  BUILD FAILED
  E:\Jenkins\jobs\workspace\AntBuilds\build.xml:336: Tests failed
  
  Total time: 16 seconds
  Build step 'Invoke Ant' marked build as failure
  Archiving artifacts
  Recording test results
  Finished: FAILURE
  </code></pre>
  
  <p>The output above is unexpected, as I have configured the build to be simply UNSTABLE if tests fail, and I have configured a JaCoCo report to be published after the tests have been run.</p>
  
  <p>Usually I would see something more like:</p>
  
  <pre><code> [junit] INFO: Closing org.springframework.context.support.GenericApplicationContext@6a38e598: display name [org.springframework.context.support.GenericApplicationContext@6a38e598]; startup date [Tue Sep 22 16:01:10 BST 2015]; root of context hierarchy
      [junit] Tests FAILED
  [jacoco:report] Loading execution data file E:\Jenkins\jobs\jacoco.exec
  [jacoco:report] Writing bundle 'Coverage Report' with 117 classes
  ...etc
  </code></pre>
  
  <p>Removing the failing tests allows the build to continue as normal:</p>
  
  <pre><code>BUILD SUCCESSFUL
  Total time: 36 seconds
  Archiving artifacts
  Recording test results
  [JaCoCo plugin] Collecting JaCoCo coverage data...
  [JaCoCo plugin] **/**.exec;**/dist/WEB-INF/classes;**/src; locations are configured
  [JaCoCo plugin] Number of found exec files for pattern **/**.exec: 1
  [JaCoCo plugin] Saving matched execfiles:  E:\Jenkins\jobs\myJob\jacoco.exec
  ... etc
  </code></pre>
  
  <p><strong>My answer so far</strong></p>
  
  <p>I have been successfully building other applications on Jenkins. The difference is that these tests are marked with a Spring application context:</p>
  
  <pre><code>@ContextConfiguration(locations="file:WEB-INF/MyJob-servlet.xml")
  @RunWith(SpringJUnit4ClassRunner.class)
  </code></pre>
  
  <p>I have been wondering if Jenkins fails to close the Application Context when tests fail, as this line appears for the failed build just before the 'BUILD FAILED' message:</p>
  
  <pre><code>Closing org.springframework.context.support.GenericApplicationContext@8a590d1: 
    startup date [Tue Sep 22 16:23:52 BST 2015]; root of context hierarchy
  </code></pre>
  
  <p>I have needed to upgrade to:</p>
  
  <ul>
  <li>JUnit-4.4</li>
  <li>spring-integration-test-2.1.3.RELEASE</li>
  </ul>
  
  <p>Is this hunch correct? If so, is this a bug with Jenkins / Spring / incompatible versions, or is my implementation incorrect?</p>