• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Testing | Deploy web applications using embedded Tomcat
  1. The POM file (pom.xml)
  2. Unit test example
  3. Web application resources (index.html, web.xml)

  1. The POM file (pom.xml)
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>mtitek.tomcat.embed.samples</groupId>
        <artifactId>mtitek-tomcat-embed-samples</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
            <maven.compiler.source>19</maven.compiler.source> <!-- maven-compiler-plugin property -->
            <maven.compiler.target>19</maven.compiler.target> <!-- maven-compiler-plugin property -->
        </properties>
    
        <dependencies>
            <!-- tomcat -->
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-core</artifactId>
                <version>11.0.0-M1</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-logging-juli</artifactId>
                <version>9.0.0.M6</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jasper</artifactId>
                <version>11.0.0-M1</version>
                <scope>test</scope>
            </dependency>
    
            <!-- shrinkwrap -->
            <dependency>
                <groupId>org.jboss.shrinkwrap</groupId>
                <artifactId>shrinkwrap-api</artifactId>
                <version>2.0.0-beta-1</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.jboss.shrinkwrap</groupId>
                <artifactId>shrinkwrap-impl-base</artifactId>
                <version>2.0.0-beta-1</version>
                <scope>test</scope>
            </dependency>
    
            <!-- ... -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpcore</artifactId>
                <version>4.4.16</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.14</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.12.0</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.11.0</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13.2</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.10.1</version>
    
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <source>${maven.compiler.source}</source> <!-- optional if maven.compiler.source property is set -->
                        <target>${maven.compiler.target}</target> <!-- optional if maven.compiler.target property is set -->
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
  2. Unit test example
    src/test/java/mtitek/tomcat/embed/samples/MyTomcatTest.java:
    package mtitek.tomcat.embed.samples;
    
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    
    import org.apache.catalina.Host;
    import org.apache.catalina.LifecycleException;
    import org.apache.catalina.startup.Tomcat;
    import org.apache.commons.io.IOUtils;
    import org.apache.http.HttpEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    import org.jboss.shrinkwrap.api.ShrinkWrap;
    import org.jboss.shrinkwrap.api.spec.WebArchive;
    import org.jboss.shrinkwrap.impl.base.exporter.zip.ZipExporterImpl;
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.rules.TemporaryFolder;
    
    public class MyTomcatTest {
        @Rule
        public TemporaryFolder temporaryFolder = new TemporaryFolder();
    
        @Test
        public void test() throws IOException, LifecycleException {
            final File newFolder = temporaryFolder.newFolder("MyTomcatTest");
            final String baseDir = newFolder.getAbsolutePath();
    
            // creating a Tomcat instance
            final Tomcat tomcat = new Tomcat();
    
            // configure tomcat
            {
                // random unassigned HTTP port
                tomcat.setPort(0);
    
                tomcat.setBaseDir(baseDir);
            }
    
            // start tomcat
            {
                tomcat.start();
            }
    
            // deploy web app
            deployWebApp(tomcat, baseDir);
    
            // stop/destroy tomcat
            {
                tomcat.stop();
                tomcat.destroy();
            }
        }
    
        private static void deployWebApp(final Tomcat tomcat, final String tomcatBaseDir) throws IOException {
            final String webAppName = "simple";
            final String archiveName = webAppName + ".war";
    
            // adding resources to WebArchive
            {
                final WebArchive webArchive = ShrinkWrap.create(WebArchive.class, archiveName);
                webArchive.addAsWebResource("index.html");
                webArchive.setWebXML("web.xml");
    
                deployWebApp(tomcat, tomcatBaseDir, webArchive, webAppName);
            }
    
            // test web app
            {
                testWebApp(tomcat, "http://localhost:" + tomcat.getConnector().getLocalPort() + '/' + webAppName);
            }
        }
    
        private static void deployWebApp(final Tomcat tomcat, final String tomcatBaseDir, final WebArchive webArchive,
                final String webAppName) throws IOException {
            final Host host = tomcat.getHost();
    
            final File rootBase = new File(tomcatBaseDir, host.getAppBase());
            Files.createDirectories(rootBase.toPath());
    
            final File docBase = new File(rootBase, webArchive.getName());
            final String docBaseAbsolutePath = docBase.getAbsolutePath();
    
            new ZipExporterImpl(webArchive).exportTo(docBase, true);
    
            final String contextPath = '/' + webAppName;
            tomcat.addWebapp(host, contextPath, docBaseAbsolutePath);
        }
    
        private static void testWebApp(final Tomcat tomcat, final String resource) throws IOException {
            final HttpGet request = new HttpGet(resource);
    
            try (final CloseableHttpClient client = HttpClientBuilder.create().build();
                    final CloseableHttpResponse response = client.execute(request)) {
                final HttpEntity entity = response.getEntity();
    
                System.out.println(IOUtils.toString(entity.getContent(), "UTF-8"));
    
                // close the content stream.
                EntityUtils.consumeQuietly(entity);
            }
        }
    }
    Notes:

    ► Adding resources to a WebArchive: addAsWebResource
    final WebArchive webArchive = ShrinkWrap.create(WebArchive.class, archiveName);
    webArchive.addAsWebResource("index.html");
    webArchive.setWebXML("web.xml");

    ► Adding resources to a WebArchive: importDirectory
    final WebArchive webArchive = ShrinkWrap.create(WebArchive.class, archiveName);
    
    webArchive.merge(ShrinkWrap.create(GenericArchive.class).as(ExplodedImporter.class).importDirectory(archivePath).as(GenericArchive.class), "/", Filters.includeAll());
  3. Web application resources (index.html, web.xml)
    src/test/resources/index.html:
    <html>
    <head>
    <title>HTML Page</title>
    </head>
    <body>
    <h1>Hello Tomcat!!!</h1>
    </body>
    </html>
    src/test/resources/web.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        metadata-complete="true">
    
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
    </web-app>
© 2025  mtitek