Class AbstractTestContainersProvider<T extends org.testcontainers.containers.GenericContainer<? extends T>>

java.lang.Object
io.micronaut.testresources.testcontainers.AbstractTestContainersProvider<T>
Type Parameters:
T - the container type
All Implemented Interfaces:
io.micronaut.core.order.Ordered, TestResourcesResolver, ToggableTestResourcesResolver
Direct Known Subclasses:
AbstractHibernateReactiveTestResourceProvider, AbstractJdbcTestResourceProvider, AbstractR2DBCTestResourceProvider, AzuriteTestResourceProvider, ConsulTestResourceProvider, CouchbaseTestResourceProvider, HazelcastTestResourceProvider, HiveMQTestResourceProvider, InfinispanTestResourceProvider, KafkaTestResourceProvider, KeycloakTestResourceProvider, LocalStackTestResourceProvider, MinioTestResourceProvider, MongoDBTestResourceProvider, Neo4jTestResourceProvider, OpenSearchTestResourceProvider, PulsarTestResourceProvider, RabbitMQTestResourceProvider, RedisClusterTestResourceProvider, RedisTestResourceProvider, SeaweedFsTestResourceProvider, SolrTestResourceProvider, VaultTestResourceProvider, WireMockTestResourceProvider

public abstract class AbstractTestContainersProvider<T extends org.testcontainers.containers.GenericContainer<? extends T>> extends Object implements ToggableTestResourcesResolver
The base class for test resources providers which spawn test containers.
  • Constructor Details

    • AbstractTestContainersProvider

      public AbstractTestContainersProvider()
  • Method Details

    • getName

      public String getName()
      Specified by:
      getName in interface ToggableTestResourcesResolver
    • getOrder

      public int getOrder()
      Specified by:
      getOrder in interface io.micronaut.core.order.Ordered
    • getSimpleName

      protected abstract String getSimpleName()
      Returns the name of the resource resolver, for example "kafka" or "mysql".
      Returns:
      the name of the resolver
    • getDefaultImageName

      protected abstract String getDefaultImageName()
      Returns the default image name.
      Returns:
      the default image name.
    • createContainer

      protected abstract T createContainer(org.testcontainers.utility.DockerImageName imageName, Map<String,Object> requestedProperties, Map<String,Object> testResourcesConfig)
      Creates the test container.
      Parameters:
      imageName - the docker image name
      requestedProperties - the resolved properties
      testResourcesConfig - the test resources configuration
      Returns:
      a container
    • shouldAnswer

      protected boolean shouldAnswer(String propertyName, Map<String,Object> requestedProperties, Map<String,Object> testResourcesConfig)
      Determines if this resolver can resolve the requested property. It is used in order to make sure that a "Postgres" resolver wouldn't provide a value if the requested container type is for MySQL, for example.
      Parameters:
      propertyName - the property to resolve
      requestedProperties - the resolved properties
      testResourcesConfig - the test resources configuration
      Returns:
      if this resolver should answer
    • resolveWithoutContainer

      protected Optional<String> resolveWithoutContainer(String propertyName, Map<String,Object> properties, Map<String,Object> testResourcesConfig)
      Lets a resolver provide a value for the requested property without triggering the creation of a test container. This can be used in case a resolver wants to check existing containers first.
      Parameters:
      propertyName - the name of the property to resolve
      properties - the properties used to resolve
      testResourcesConfig - the test resources configuration
      Returns:
      a resolved property
    • resolve

      public final Optional<String> resolve(String propertyName, Map<String,Object> properties, Map<String,Object> testResourcesConfig)
      Description copied from interface: TestResourcesResolver
      Resolves the given property.
      Specified by:
      resolve in interface TestResourcesResolver
      Parameters:
      propertyName - the property to resolve
      properties - the resolved required properties
      testResourcesConfig - the test resources configuration
      Returns:
      the resolved property or empty if not found
    • getContainerOwnerKey

      protected String getContainerOwnerKey(String propertyName, Map<String,Object> properties, Map<String,Object> testResourcesConfig)
      Returns the owner key used to scope cached containers for this resolver. Subclasses may override to share a physical container across multiple logical consumers, but should keep the returned key stable for equivalent requests.
      Parameters:
      propertyName - the property being resolved
      properties - the resolved properties for the request
      testResourcesConfig - the test resources configuration
      Returns:
      the owner key used for container reuse
    • getContainerQuery

      protected Map<String,Object> getContainerQuery(String propertyName, Map<String,Object> properties, Map<String,Object> testResourcesConfig)
      Returns the query object used to look up or create a cached container. Subclasses may override to normalize request-specific properties into a stable physical-resource identity while preserving any keys needed for safe reuse.
      Parameters:
      propertyName - the property being resolved
      properties - the resolved properties for the request
      testResourcesConfig - the test resources configuration
      Returns:
      the container query used for cache lookup
    • configureContainer

      protected void configureContainer(T container, Map<String,Object> properties, Map<String,Object> testResourcesConfig)
    • getDefaultStartupTimeout

      protected Optional<Duration> getDefaultStartupTimeout(Map<String,Object> properties, Map<String,Object> testResourcesConfig)
      Returns the default startup timeout for this provider before user metadata is applied.
      Parameters:
      properties - the resolved properties for the request
      testResourcesConfig - the test resources configuration
      Returns:
      the provider default startup timeout, if any
    • prepareContainer

      protected void prepareContainer(String propertyName, T container, Map<String,Object> properties, Map<String,Object> testResourcesConfig)
    • resolveProperty

      protected Optional<String> resolveProperty(String propertyName, T container, Map<String,Object> properties, Map<String,Object> testResourcesConfig)
      Resolves the requested property from the started container with access to the full requested-property map and test-resources configuration. Subclasses may override when the resolved value depends on request metadata in addition to the container itself.
      Parameters:
      propertyName - the property being resolved
      container - the started container
      properties - the resolved properties for the request
      testResourcesConfig - the test resources configuration
      Returns:
      the resolved value, if any
    • resolveProperty

      protected abstract Optional<String> resolveProperty(String propertyName, T container)
    • executeInContainer

      protected final void executeInContainer(String failureMessage, AbstractTestContainersProvider.ContainerCommand command)
      Executes a command inside a running container and converts failures into a consistent IllegalStateException. Interrupted executions preserve the current thread interrupt status before the exception is raised.
      Parameters:
      failureMessage - the message to use when the command fails
      command - the command to execute
    • stringOrNull

      protected final String stringOrNull(Object value)