shared classloader with embedded Tomcat 8
<p>I have upgraded tomcat from version 7.0.34 to version 8.0.33, and since then I have been facing a problem to share the web application context and Junit context.</p>

<p>I have a web application with singleton class that gathers statistic data about the web application. I also have Junit  that runs the web application in embedded tomcat. the Junit queries the web application and then checks the statistic data.</p>

<p>I try to make a simple example:</p>

<p>the singleton:</p>

<pre><code>  public class Counter {

  private static Counter instance;
  private AtomicLong counter;

  private Counter(){}

  public static Counter getInstance(){
    if(instance == null){
      synchronized (Counter.class) {
        if(instance == null){
          instance = new Counter();
        }
      }
    }

    return instance;
  }

  public long incrementAndGet(){
    return counter.incrementAndGet();
  }

  public long getValue(){
    return counter.get();
  }

}
</code></pre>

<p>the servlet:</p>

<pre><code>@WebServlet(name="servlet",loadOnStartup=1, urlPatterns="/servletTest")
public class Servlet extends HttpServlet{

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      resp.getWriter().write("Hi, you are the #" + Counter.getInstance().incrementAndGet() + " visitor");
  }
}
</code></pre>

<p>contextListener:</p>

<pre><code>public class MyContextListener implements ServletContextListener{
  @Override
  public void contextDestroyed(ServletContextEvent arg0) {}

  @Override
  public void contextInitialized(ServletContextEvent arg0) {
    Counter.getInstance().incrementAndGet(); 
  }
}
</code></pre>

<p>Test unit:</p>

<pre><code>  public void mainTest() throws ServletException, LifecycleException{
    Tomcat tomcat = new Tomcat();

   tomcat.setPort(50000);
   StandardContext ctx = (StandardContext) tomcat.addWebapp("/fe", System.getProperty("FEBaseDir")); //The FEBaseDir property is supposed to be taken from Maven build using 'test' profile

   tomcat.start();

   Counter.getInstance().getValue();

  }
</code></pre>

<p>when I used Tomcat 7, everything worked fine. but since  I upgraded tomcat to tomcat 8.0.33, It hasn't been working. the singleton class with the static data loads twice. first by the tomcat and then by the Junit itself.</p>

<p>I have tried to pass tomcat a classloader but it doesn't work.</p>

<pre><code> public void mainTest() throws ServletException, LifecycleException{
    Tomcat tomcat = new Tomcat();

   tomcat.setPort(50000);
   StandardContext ctx = (StandardContext) tomcat.addWebapp("/fe", System.getProperty("FEBaseDir")); //The FEBaseDir property is supposed to be taken from Maven build using 'test' profile

   ctx.setCrossContext(true);
   ctx.setLoader((Loader) new WebappLoader(Thread.currentThread().getContextClassLoader()));

   ctx.setParentClassLoader(Thread.currentThread().getContextClassLoader());

   tomcat.getEngine().setParentClassLoader(Thread.currentThread().getContextClassLoader());
   tomcat.getHost().setParentClassLoader(Thread.currentThread().getContextClassLoader());
   tomcat.getService().setParentClassLoader(Thread.currentThread().getContextClassLoader());
   tomcat.getServer().setParentClassLoader(Thread.currentThread().getContextClassLoader());
   tomcat.start();

   Counter.getInstance().getValue();

  }
</code></pre>

<p>What am I doing wrong?</p>