我目前需要创建一个服务器来运行一些单元测试。为了简化这个过程,我想在运行单元测试(使用@BeforeClass表示法)之前,将Tomcat嵌入到我的代码中,并加载Tomcat的一个实例(反过来加载我的WAR文件)。
我的问题是如何将WAR文件部署到嵌入式Tomcat中?
正如您可能注意到的,我不能使用tomcat maven插件,因为我想让它与自动化测试一起运行。
发布于 2015-12-03 20:20:14
此代码适用于Tomcat 8.0:
File catalinaHome = new File("..."); // folder must exist
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080); // HTTP port
tomcat.setBaseDir(catalinaHome.getAbsolutePath());
tomcat.getServer().addLifecycleListener(new VersionLoggerListener()); // nice to have您现在有两个选择。在catalinaHome/webapps中自动部署任何web应用程序
// This magic line makes Tomcat look for WAR files in catalinaHome/webapps
// and automatically deploy them
tomcat.getHost().addLifecycleListener(new HostConfig());或者,您可以手动添加WAR归档。注:它们可以在硬盘上的任何位置。
// Manually add WAR archives to deploy.
// This allows to define the order in which the apps are discovered
// plus the context path.
File war = new File(...);
tomcat.addWebapp("/contextPath", war.getAbsolutePath());https://stackoverflow.com/questions/17809914
复制相似问题