Coder Social home page Coder Social logo

exys666 / testcompose Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 1.0 18 KB

JUnit5 extension for managing docker-compose files during test execution

Home Page: http://testcompose.com

License: MIT License

Kotlin 100.00%
junit5 junit docker docker-compose java kotlin unit-testing

testcompose's Introduction

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

testcompose's People

Contributors

exys666 avatar

Stargazers

emeraldjava avatar Davor Sauer avatar Yonatan Wilkof avatar  avatar

Watchers

James Cloos avatar  avatar

Forkers

ywilkof

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.