title: Tomcat Keeps Running After Integration Test Finished
body: <p><em>I'm sorry for the complete rewrite in case you read this earlier...</em></p>
  
  <p>I migrated a project to a Spring-Boot project and executing an integration test results in Tomcat running after it finished. This is true for running it in Eclipse and Maven. The downside of Maven is that the building process discontinues and only Ctrl+C helps, which actually also stops Maven.</p>
  
  <p>Here's the plug-in stuff from the <code>pom.xml</code>:</p>
  
  <pre><code>&lt;plugins&gt;
    &lt;plugin&gt;
      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
      &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
      &lt;version&gt;3.1&lt;/version&gt;
      &lt;configuration&gt;
        &lt;source&gt;${java.version}&lt;/source&gt;
        &lt;target&gt;${java.version}&lt;/target&gt;
      &lt;/configuration&gt;
    &lt;/plugin&gt;
  
    &lt;plugin&gt;
      &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
      &lt;configuration&gt;
        &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
      &lt;/configuration&gt;
    &lt;/plugin&gt;
  
    &lt;plugin&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
      &lt;executions&gt;
        &lt;execution&gt;
          &lt;goals&gt;
            &lt;goal&gt;repackage&lt;/goal&gt;
          &lt;/goals&gt;
          &lt;configuration&gt;
            &lt;finalName&gt;at.a1.iap.spagat.aggregator&lt;/finalName&gt;
          &lt;/configuration&gt;
        &lt;/execution&gt;
      &lt;/executions&gt;
    &lt;/plugin&gt;
  
    &lt;plugin&gt;
      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
      &lt;artifactId&gt;maven-failsafe-plugin&lt;/artifactId&gt;
      &lt;version&gt;2.8&lt;/version&gt;
      &lt;executions&gt;
        &lt;execution&gt;
          &lt;id&gt;integration-test&lt;/id&gt;
          &lt;goals&gt;
            &lt;goal&gt;integration-test&lt;/goal&gt;
          &lt;/goals&gt;
          &lt;configuration&gt;
            &lt;!-- required to make four soapui-test-classes work --&gt;
            &lt;forkMode&gt;pertest&lt;/forkMode&gt;
            &lt;includes&gt;
              &lt;include&gt;**/*ITest.java&lt;/include&gt;
            &lt;/includes&gt;
            &lt;!-- no need to exclude and if you exclude no tests will be
              run &lt;excludes&gt; &lt;exclude&gt;**/*Test.java&lt;/exclude&gt; &lt;/excludes&gt; --&gt;
          &lt;/configuration&gt;
        &lt;/execution&gt;
        &lt;execution&gt;
          &lt;id&gt;verify&lt;/id&gt;
          &lt;goals&gt;
            &lt;goal&gt;verify&lt;/goal&gt;
          &lt;/goals&gt;
        &lt;/execution&gt;
      &lt;/executions&gt;
    &lt;/plugin&gt;
  
    &lt;plugin&gt;
      &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
      &lt;artifactId&gt;axistools-maven-plugin&lt;/artifactId&gt;
      &lt;version&gt;1.4&lt;/version&gt;
      &lt;configuration&gt;
        &lt;mappings&gt;
          &lt;mapping&gt;
            &lt;namespace&gt;http://www.agama.tv/ws/emp&lt;/namespace&gt;
            &lt;targetPackage&gt;at.a1.iap.spagat.aggregator.external.agama&lt;/targetPackage&gt;
          &lt;/mapping&gt;
        &lt;/mappings&gt;
  
        &lt;sourceDirectory&gt;${basedir}/src/main/resources/wsdl&lt;/sourceDirectory&gt;
        &lt;outputDirectory&gt;${basedir}/src/gen/java&lt;/outputDirectory&gt;
        &lt;testCases&gt;false&lt;/testCases&gt;
        &lt;serverSide&gt;true&lt;/serverSide&gt;
        &lt;subPackageByFileName&gt;false&lt;/subPackageByFileName&gt;
      &lt;/configuration&gt;
      &lt;executions&gt;
        &lt;execution&gt;
          &lt;goals&gt;
            &lt;goal&gt;wsdl2java&lt;/goal&gt;
          &lt;/goals&gt;
        &lt;/execution&gt;
      &lt;/executions&gt;
    &lt;/plugin&gt;
  &lt;/plugins&gt;
  </code></pre>
  
  <p>After lots of fiddling around I noticed that these two beans cause this (actually just one of them):</p>
  
  <pre><code>  @Bean
    public ServletRegistrationBean servletWs(ServletContext servletContext) {
      WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
      DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext);
  
      ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "/ws/*");
      servletRegistrationBean.setLoadOnStartup(1);
      servletRegistrationBean.addInitParameter("dispatchOptionsRequest", "true");
      servletRegistrationBean.setName("general-dispatcher");
  
      return servletRegistrationBean;
    }
  
    @Bean
    public ServletRegistrationBean servletUi(ServletContext servletContext) {
      WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
      DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext);
  
      ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "/ui/*");
      servletRegistrationBean.setLoadOnStartup(1);
      servletRegistrationBean.setName("pagestuff-dispatcher");
  
      FilterRegistration.Dynamic welcomeFilter = servletContext.addFilter("WelcomeFilter", new WelcomeFilter("./ui/index"));
      welcomeFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/");
  
      return servletRegistrationBean;
    }
  </code></pre>
  
  <p>The magic line is</p>
  
  <pre><code>servletRegistrationBean.setLoadOnStartup(1);
  </code></pre>
  
  <p>If I comment it out Tomcat stops as expected. So why shouldn't I be doing this and how should I do it instead?</p>
  
  <p>If you need further excerpts of the code or other info don't hesitate to ask.</p>
  
  <p><em>This is Spring-Boot 1.4.1 with embedded Tomcat</em></p>