Test Compose

JUnit5 extension for managing docker-compose files during test execution.

License

License

Categories

Categories

JUnit Unit Testing
GroupId

GroupId

com.testcompose
ArtifactId

ArtifactId

junit-extension
Last Version

Last Version

0.5.0
Release Date

Release Date

Type

Type

jar
Description

Description

Test Compose
JUnit5 extension for managing docker-compose files during test execution.
Project URL

Project URL

http://testcompose.com
Source Code Management

Source Code Management

http://github.com/exys666/testcompose/tree/master

Download junit-extension

How to add to project

<!-- https://jarcasting.com/artifacts/com.testcompose/junit-extension/ -->
<dependency>
    <groupId>com.testcompose</groupId>
    <artifactId>junit-extension</artifactId>
    <version>0.5.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.testcompose/junit-extension/
implementation 'com.testcompose:junit-extension:0.5.0'
// https://jarcasting.com/artifacts/com.testcompose/junit-extension/
implementation ("com.testcompose:junit-extension:0.5.0")
'com.testcompose:junit-extension:jar:0.5.0'
<dependency org="com.testcompose" name="junit-extension" rev="0.5.0">
  <artifact name="junit-extension" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.testcompose', module='junit-extension', version='0.5.0')
)
libraryDependencies += "com.testcompose" % "junit-extension" % "0.5.0"
[com.testcompose/junit-extension "0.5.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib jar 1.3.31
org.junit.jupiter : junit-jupiter-api jar 5.0.2

Project Modules

There are no modules declared in this project.

Test Compose

JUnit5 extension for managing docker-compose files during test execution

Requirements

Usage

Maven dependency

Maven central, see

<pom>
    
    <dependencies>
        <dependency>
            <groupId>com.testcompose</groupId>
            <artifactId>junit-extension</artifactId>
            <version>0.3.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</pom>

or JitPack release, read more

<pom>

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>
    
    <dependencies>
        <dependency>
            <groupId>com.github.exys666</groupId>
            <artifactId>testcompose</artifactId>
            <version>0.2.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</pom>

Maven Central repository release coming soon!

Simple usage

src/test/resources/docker-compose-yml

version: '2.1'
services:
  postgres:
    image: 'postgres:9.6'
    environment:
      POSTGRES_DB: 'db'
      POSTGRES_USER: 'user'
      POSTGRES_PASSWORD: 'password'
    ports:
      - '5432'

src/test/java/com/mydomain/SomeIntegrationTest.java

package com.mydomain;

import com.testcompose.TestComposeExtension;
import com.testcompose.annotation.Await;
import com.testcompose.annotation.Compose;
import com.testcompose.annotation.Port;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.Test;

@ExtendWith(TestComposeExtension.class)
@Compose(
        id = "my-id",
        exportPorts = @Port(container = "postgres", port = 5432, as = "postgres.port"),
        waitFor = @Await(container = "postgres", message = "database system is ready to accept connections")
)
class SomeIntegrationTest {
    
    @Test
    void someTest() {
        String port = System.getProperty("postgres.port");
        ...
    }    
}

Attaching to running container composition

Extension can attach to running docker composition. In order to do that docker compose needs to be run with same id as in @Compose annotation

docker-compose \
    -f src/test/resources/docker-compose.yml \
    -p my-id up \
    --build

Integrating with Spring

src/test/java/com/mydomain/SpringIntegrationTest.java

package com.mydomain;

import com.testcompose.TestComposeExtension;
import com.testcompose.annotation.Await;
import com.testcompose.annotation.Compose;
import com.testcompose.annotation.Port;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(TestComposeExtension.class)
@ExtendWith(SpringExtension.class)
@Compose(
        id = "my-id",
        exportPorts = @Port(container = "postgres", port = 5432, as = "postgres.port"),
        waitFor = @Await(container = "postgres", message = "database system is ready to accept connections")
)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class SpringIntegrationTest {
    
    @Test
    void springTest() {
        ...
    }    
}

src/test/resources/application-test.properties

db.url=postgres://user:[email protected]:${postgres.port}/db

Versions

Version
0.5.0
0.4.0
0.3.1
0.3.0