Dear all,

I use org.apache.catalina.startup.Tomcat to do unit test,  but the instance
of which cannot be started or stopped correctly.

I start and stop embedding tomcat for each test case, but first test can be
passed, the second one will be failed by 503 Service,  unavailable, the
tomcat.start() method has been invoked, but the tomcat return 503 error.

My code for tomcat start & stop:

public static Tomcat startServer() throws LifecycleException {
Tomcat tomcat = new Tomcat();
tomcat.setPort(8088);
 Context context = tomcat.addContext("",
System.getProperty("java.io.tmpdir"));
 Wrapper servlet = Tomcat.addServlet(
context,
MyServlet.class.getSimpleName(),
MyServlet.class.getName());
 context.addServletMapping("/*", servlet.getName());
tomcat.start();
return tomcat;
}

public static void stop(Tomcat tomcat) {
if (tomcat == null || tomcat.getServer() == null) {
return;
}
        if (tomcat.getServer().getState() != LifecycleState.DESTROYED) {
            if (tomcat.getServer().getState() != LifecycleState.STOPPED) {
                try {
tomcat.stop();
} catch (LifecycleException e) {
LOGGER.error("Stop tomcat error.", e);
}
            }
            try {
tomcat.destroy();
} catch (LifecycleException e) {
LOGGER.error("Destroy tomcat error.", e);
}
        }
}
}

Thanks.

Zijian

Reply via email to