Class ApplicationContainer

  • All Implemented Interfaces:
    java.lang.AutoCloseable, org.junit.rules.TestRule, org.testcontainers.containers.Container<ApplicationContainer>, org.testcontainers.containers.ContainerState, org.testcontainers.containers.traits.LinkableContainer, org.testcontainers.containers.wait.strategy.WaitStrategyTarget, org.testcontainers.lifecycle.Startable

    public class ApplicationContainer
    extends org.testcontainers.containers.GenericContainer<ApplicationContainer>
    Represents a MicroProfile, JavaEE, or JakartaEE application running inside a Docker container.
    • Nested Class Summary

      • Nested classes/interfaces inherited from class org.testcontainers.containers.GenericContainer

        org.testcontainers.containers.GenericContainer.AbstractWaitStrategy
      • Nested classes/interfaces inherited from interface org.testcontainers.containers.Container

        org.testcontainers.containers.Container.ExecResult
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String MP_HEALTH_READINESS_PATH
      A path representing the MicroProfile Health 2.0 readiness check
      • Fields inherited from class org.testcontainers.containers.GenericContainer

        CONTAINER_RUNNING_TIMEOUT_SEC, containerId, containerName, dependencies, dockerClient, dockerDaemonInfo, INTERNAL_HOST_HOSTNAME, waitStrategy
      • Fields inherited from interface org.testcontainers.containers.ContainerState

        STATE_HEALTHY
    • Constructor Summary

      Constructors 
      Constructor Description
      ApplicationContainer()
      Builds an instance based on a Dockerfile located at
      ApplicationContainer​(java.lang.String dockerImageName)
      Builds an instance based on an existing docker image.
      ApplicationContainer​(java.nio.file.Path dockerfilePath)
      Builds an instance using the supplied Dockerfile path
      ApplicationContainer​(java.util.concurrent.Future<java.lang.String> dockerImageName)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void configure()  
      protected void containerIsStarting​(com.github.dockerjava.api.command.InspectContainerResponse containerInfo)  
      protected void doStart()  
      java.lang.String getApplicationURL()  
      java.lang.String getBaseURL()  
      com.github.dockerjava.api.command.InspectContainerResponse getContainerInfo()  
      java.lang.String getContainerIpAddress()  
      java.lang.String getDockerImageName()  
      java.lang.Integer getMappedPort​(int originalPort)  
      ServerAdapter getServerAdapter()  
      boolean isCreated()  
      boolean isHealthy()  
      boolean isRunning()  
      void setContainerIpAddress​(java.lang.String ipAddress)  
      void setExposedPorts​(java.util.List<java.lang.Integer> exposedPorts)  
      void setFirstMappedPort​(int port)  
      void setWaitStrategy​(org.testcontainers.containers.wait.strategy.WaitStrategy waitStrategy)  
      java.lang.String toStringSimple()  
      ApplicationContainer waitingFor​(org.testcontainers.containers.wait.strategy.WaitStrategy waitStrategy)  
      ApplicationContainer withAppContextRoot​(java.lang.String appContextRoot)  
      ApplicationContainer withHttpPort​(int httpPort)  
      ApplicationContainer withMpRestClient​(java.lang.Class<?> restClientClass, java.lang.String hostUri)
      Configures the application container with the supplied MicroProfile REST Client class that will reference the supplied hostUrl
      ApplicationContainer withMpRestClient​(java.lang.String restClientClass, java.lang.String hostUri)
      Configures the application container with the supplied MicroProfile REST Client class that will reference the supplied hostUrl
      ApplicationContainer withReadinessPath​(java.lang.String readinessUrl)
      Sets the path to be used to determine container readiness.
      ApplicationContainer withReadinessPath​(java.lang.String readinessUrl, int timeoutSeconds)
      Sets the path to be used to determine container readiness.
      ApplicationContainer withReadinessPath​(java.lang.String readinessUrl, int timeoutSeconds, java.lang.Integer port)
      Sets the path to be used to determine container readiness.
      ApplicationContainer withReuse​(boolean reusable)  
      • Methods inherited from class org.testcontainers.containers.GenericContainer

        addEnv, addExposedPort, addExposedPorts, addFileSystemBind, addFixedExposedPort, addFixedExposedPort, addLink, apply, canBeReused, containerIsCreated, containerIsStarted, containerIsStarted, containerIsStarting, containerIsStopped, containerIsStopping, copyFileFromContainer, createVolumeDirectory, dependsOn, dependsOn, dependsOn, equals, failed, fetchDockerDaemonInfo, finished, getBinds, getCommandParts, getContainerId, getContainerName, getCopyToFileContainerPathMap, getCreateContainerCmdModifiers, getDependencies, getDockerClient, getDockerDaemonInfo, getEnv, getEnvMap, getExposedPorts, getExtraHosts, getImage, getIpAddress, getLabels, getLinkedContainers, getLivenessCheckPort, getLivenessCheckPortNumbers, getLivenessCheckPorts, getLogConsumers, getNetwork, getNetworkAliases, getNetworkMode, getPortBindings, getShmSize, getStartupAttempts, getStartupCheckStrategy, getTestHostIpAddress, getTmpFsMapping, getVolumesFroms, getWaitStrategy, getWorkingDirectory, hashCode, isPrivilegedMode, isShouldBeReused, logger, setBinds, setCommand, setCommand, setCommandParts, setCopyToFileContainerPathMap, setDockerImageName, setEnv, setExtraHosts, setImage, setLabels, setLinkedContainers, setLogConsumers, setNetwork, setNetworkAliases, setNetworkMode, setPortBindings, setPrivilegedMode, setShmSize, setStartupAttempts, setStartupCheckStrategy, setTmpFsMapping, setVolumesFroms, setWorkingDirectory, start, starting, stop, succeeded, toString, waitUntilContainerStarted, withClasspathResourceMapping, withClasspathResourceMapping, withCommand, withCommand, withCopyFileToContainer, withCreateContainerCmdModifier, withEnv, withEnv, withExposedPorts, withExtraHost, withFileSystemBind, withImagePullPolicy, withLabel, withLabels, withLogConsumer, withMinimumRunningDuration, withNetwork, withNetworkAliases, withNetworkMode, withPrivilegedMode, withSharedMemorySize, withStartupAttempts, withStartupCheckStrategy, withStartupTimeout, withTmpFs, withVolumesFrom, withWorkingDirectory
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface org.testcontainers.containers.Container

        addFileSystemBind, followOutput, followOutput, self, withEnv, withFileSystemBind
      • Methods inherited from interface org.testcontainers.containers.ContainerState

        copyFileFromContainer, copyFileToContainer, copyFileToContainer, execInContainer, execInContainer, getBoundPortNumbers, getCurrentContainerInfo, getFirstMappedPort, getLogs, getLogs
      • Methods inherited from interface org.testcontainers.lifecycle.Startable

        close
    • Field Detail

      • MP_HEALTH_READINESS_PATH

        public static final java.lang.String MP_HEALTH_READINESS_PATH
        A path representing the MicroProfile Health 2.0 readiness check
        See Also:
        Constant Field Values
    • Constructor Detail

      • ApplicationContainer

        public ApplicationContainer()
        Builds an instance based on a Dockerfile located at
         ${user.dir}/Dockerfile}
         
        or
         ${user.dir}/src/main/docker/Dockerfile
         
        . If no Dockerfile can be discovered, a ServerAdapter may be used to supply a default Dockerfile. A docker build will be performed before the resulting container image is started.
      • ApplicationContainer

        public ApplicationContainer​(java.nio.file.Path dockerfilePath)
        Builds an instance using the supplied Dockerfile path
        Parameters:
        dockerfilePath - A Path indicating the Dockerfile to be used to build the container image. A docker build will be performed before the resulting container image is started.
      • ApplicationContainer

        public ApplicationContainer​(java.util.concurrent.Future<java.lang.String> dockerImageName)
      • ApplicationContainer

        public ApplicationContainer​(java.lang.String dockerImageName)
        Builds an instance based on an existing docker image.
        Parameters:
        dockerImageName - The docker image to be used for this instance
    • Method Detail

      • configure

        protected void configure()
        Overrides:
        configure in class org.testcontainers.containers.GenericContainer<ApplicationContainer>
      • setContainerIpAddress

        public void setContainerIpAddress​(java.lang.String ipAddress)
      • setFirstMappedPort

        public void setFirstMappedPort​(int port)
      • containerIsStarting

        protected void containerIsStarting​(com.github.dockerjava.api.command.InspectContainerResponse containerInfo)
        Overrides:
        containerIsStarting in class org.testcontainers.containers.GenericContainer<ApplicationContainer>
      • doStart

        protected void doStart()
        Overrides:
        doStart in class org.testcontainers.containers.GenericContainer<ApplicationContainer>
      • isCreated

        public boolean isCreated()
      • isHealthy

        public boolean isHealthy()
      • isRunning

        public boolean isRunning()
      • getContainerIpAddress

        public java.lang.String getContainerIpAddress()
      • getMappedPort

        public java.lang.Integer getMappedPort​(int originalPort)
      • setExposedPorts

        public void setExposedPorts​(java.util.List<java.lang.Integer> exposedPorts)
        Specified by:
        setExposedPorts in interface org.testcontainers.containers.Container<ApplicationContainer>
        Overrides:
        setExposedPorts in class org.testcontainers.containers.GenericContainer<ApplicationContainer>
      • withHttpPort

        public ApplicationContainer withHttpPort​(int httpPort)
        Parameters:
        httpPort - The HTTP port used for the ApplicationContainer. This will set the port used to construct the base application URL for all injected RESTClients as well as the port used to determine container readiness (unless specified otherwise in withReadinessPath(String, int, Integer)
        Returns:
        the current instance
      • withAppContextRoot

        public ApplicationContainer withAppContextRoot​(java.lang.String appContextRoot)
        Parameters:
        appContextRoot - the application context root. The protocol, hostname, and port do not need to be included in the appContextRoot parameter. For example, an application "foo.war" is available at http://localhost:8080/foo/ the context root can be set using withAppContextRoot("/foo") Setting the app context root effects getApplicationURL() which in turn effects the base URL of all injected RESTClients.
        Returns:
        the current instance
      • withReadinessPath

        public ApplicationContainer withReadinessPath​(java.lang.String readinessUrl)
        Sets the path to be used to determine container readiness. The readiness check will timeout after a sensible amount of time has elapsed. If unspecified, the readiness path with defailt to the application context root
        Parameters:
        readinessUrl - The HTTP endpoint to be polled for readiness. Once the endpoint returns HTTP 200 (OK), the container is considered to be ready.
        Returns:
        the current instance
      • withReadinessPath

        public ApplicationContainer withReadinessPath​(java.lang.String readinessUrl,
                                                      int timeoutSeconds)
        Sets the path to be used to determine container readiness. The readiness check will timeout after a sensible amount of time has elapsed. If unspecified, the readiness path with defailt to the application context root
        Parameters:
        readinessUrl - The HTTP endpoint to be polled for readiness. Once the endpoint returns HTTP 200 (OK), the container is considered to be ready.
        timeoutSeconds - The amount of time (in seconds) to wait for the container to be ready.
        Returns:
        the current instance
      • withReadinessPath

        public ApplicationContainer withReadinessPath​(java.lang.String readinessUrl,
                                                      int timeoutSeconds,
                                                      java.lang.Integer port)
        Sets the path to be used to determine container readiness. The readiness check will timeout after a sensible amount of time has elapsed. If unspecified, the readiness path with defailt to the application context root
        Parameters:
        readinessUrl - The HTTP endpoint to be polled for readiness. Once the endpoint returns HTTP 200 (OK), the container is considered to be ready.
        timeoutSeconds - The amount of time (in seconds) to wait for the container to be ready.
        port - The port that should be used for the readiness check.
        Returns:
        the current instance
      • waitingFor

        public ApplicationContainer waitingFor​(org.testcontainers.containers.wait.strategy.WaitStrategy waitStrategy)
        Specified by:
        waitingFor in interface org.testcontainers.containers.Container<ApplicationContainer>
        Overrides:
        waitingFor in class org.testcontainers.containers.GenericContainer<ApplicationContainer>
      • setWaitStrategy

        public void setWaitStrategy​(org.testcontainers.containers.wait.strategy.WaitStrategy waitStrategy)
        Specified by:
        setWaitStrategy in interface org.testcontainers.containers.Container<ApplicationContainer>
        Overrides:
        setWaitStrategy in class org.testcontainers.containers.GenericContainer<ApplicationContainer>
      • withMpRestClient

        public ApplicationContainer withMpRestClient​(java.lang.Class<?> restClientClass,
                                                     java.lang.String hostUri)
        Configures the application container with the supplied MicroProfile REST Client class that will reference the supplied hostUrl
        Parameters:
        restClientClass - The MicroProfile REST Client interface, which must be annotated with @RegisterRestClient
        hostUri - The URL that the restClientClass will act as a REST client for
        Returns:
        the current instance
        Throws:
        java.lang.IllegalArgumentException - If the provided restClientClass is not an interface or not annotated with @RegisterRestClient
        java.lang.IllegalArgumentException - If hostUri is not a valid URI
      • withMpRestClient

        public ApplicationContainer withMpRestClient​(java.lang.String restClientClass,
                                                     java.lang.String hostUri)
        Configures the application container with the supplied MicroProfile REST Client class that will reference the supplied hostUrl
        Parameters:
        restClientClass - The MicroProfile REST Client class
        hostUri - The URL that the restClientClass will act as a REST client for
        Returns:
        the current instance
        Throws:
        java.lang.IllegalArgumentException - If hostUri is not a valid URI
      • getApplicationURL

        public java.lang.String getApplicationURL()
        Returns:
        The URL where the application is currently running at. The application URL is comprised of the baseURL (as defined by getBaseURL()) concatenated with the appContextRoot (as defined by withAppContextRoot(String). This will be the base URL for all injected RESTClients
      • getBaseURL

        public java.lang.String getBaseURL()
        Returns:
        The base URL of the application container. For example, if the application is running on 'localhost' on port 8080 (inside the container), port 8080 inside of the container will be mapped to a random external port (usually in the 32XXX range). The base URL would be something like:

        http://<container-ip-address>:<mapped-port>

      • getContainerInfo

        public com.github.dockerjava.api.command.InspectContainerResponse getContainerInfo()
        Specified by:
        getContainerInfo in interface org.testcontainers.containers.ContainerState
        Overrides:
        getContainerInfo in class org.testcontainers.containers.GenericContainer<ApplicationContainer>
      • getDockerImageName

        public java.lang.String getDockerImageName()
        Specified by:
        getDockerImageName in interface org.testcontainers.containers.Container<ApplicationContainer>
        Overrides:
        getDockerImageName in class org.testcontainers.containers.GenericContainer<ApplicationContainer>
      • getServerAdapter

        public ServerAdapter getServerAdapter()
        Returns:
        The ServerAdapter that is currently applied for this instance
      • toStringSimple

        public java.lang.String toStringSimple()