FaustXVI / junit5-docker

Start docker containers from your junit tests
Apache License 2.0
54 stars 7 forks source link

Spring Boot Integration Tests support ? #100

Open robert-malai opened 7 years ago

robert-malai commented 7 years ago

Might have been already touched upon the matter (please point me in the right direction if so), but I'm having troubles using this with Spring boot integration tests.

Code excerpt:

@Docker(image = "postgres", ports = @Port(inner = 5432, exposed = 5432),
    environments = {
        @Environment(key = "POSTGRES_DB", value = "test-db"),
        @Environment(key = "POSTGRES_USER", value = "test-user"),
        @Environment(key = "POSTGRES_PASSWORD", value = "test-password")
    },
    waitFor = @WaitFor(value = "PostgreSQL init process complete;"))
@SpringBootTest
@ExtendWith(SpringExtension.class)
@TestInstance(Lifecycle.PER_CLASS)
class UserControllerIntegrationTest {
    private @Autowired WebApplicationContext wac;
    private @Autowired UserRepository userRepository;

    private MockMvc mockMvc;
    private ObjectMapper objectMapper = new ObjectMapper();

    @BeforeAll
    void setUpTest () {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
        objectMapper.setFilterProvider(new SimpleFilterProvider()
            .setFailOnUnknownId(false)
        );
    }

    @Test
    @WithUserDetails("batman@gotham-city.com")
    void getUserListTest() throws Exception {
        mockMvc.perform(get("/users"))
            .andDo(print())
            .andExpect(status().is2xxSuccessful())
            .andExpect(jsonPath("$.content").isArray())
            .andExpect(jsonPath("$.content").isNotEmpty());
    }
}

What I have observed is that when I'm adding @SpringBootTest annotation, the DockerExtension doesn't kick in no more - there is no traceable call to DockerExtension.beforeAll. Without it, it works, but then my tests config will fail...

I would love to be able to use this annotation - @Docker with my integration tests. Please help ?

FaustXVI commented 7 years ago

Hi Robert,

Thanks for the issue.

The goal of this extension is clearly to be able to do that. It's definitely a bug. Which versions (docker, junit, junit-docker) are you using?

Thanks

On 5 Sep 2017 01:46, "Robert Mălai" notifications@github.com wrote:

Might have been already touched upon the matter (please point me in the right direction if so), but I'm having troubles using this with Spring boot integration tests.

Code excerpt:

@Docker(image = "postgres", ports = @Port(inner = 5432, exposed = 5432), environments = { @Environment(key = "POSTGRES_DB", value = "test-db"), @Environment(key = "POSTGRES_USER", value = "test-user"), @Environment(key = "POSTGRES_PASSWORD", value = "test-password") }, waitFor = @WaitFor(value = "PostgreSQL init process complete;")) @SpringBootTest @ExtendWith(SpringExtension.class) @TestInstance(Lifecycle.PER_CLASS) class UserControllerIntegrationTest { private @Autowired WebApplicationContext wac; private @Autowired UserRepository userRepository;

private MockMvc mockMvc;
private ObjectMapper objectMapper = new ObjectMapper();

@BeforeAll
void setUpTest () {
    mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    objectMapper.setFilterProvider(new SimpleFilterProvider()
        .setFailOnUnknownId(false)
    );
}

@Test
@WithUserDetails("batman@gotham-city.com")
void getUserListTest() throws Exception {
    mockMvc.perform(get("/users"))
        .andDo(print())
        .andExpect(status().is2xxSuccessful())
        .andExpect(jsonPath("$.content").isArray())
        .andExpect(jsonPath("$.content").isNotEmpty());
}

}

What I have observed is that when I'm adding @SpringBootTest annotation, the DockerExtension doesn't kick in no more - there is no traceable call to DockerExtension.beforeAll. Without it, it works, but then my tests config will fail...

I would love to be able to use this annotation - @Docker with my integration tests. Please help ?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/FaustXVI/junit5-docker/issues/100, or mute the thread https://github.com/notifications/unsubscribe-auth/AA-EH4sDXdwZs_N27sYxHzMko9QyraGfks5sfIvagaJpZM4PMTiC .

robert-malai commented 7 years ago

Hello. Thanks for looking into this.

Excerpt of my pom.xml file:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.M3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
            <version>4.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.0-RC2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.0.0-RC2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.faustxvi</groupId>
            <artifactId>junit5-docker</artifactId>
            <version>1.0.0-RC5</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

Docker: Version 17.06.1-ce-mac24 (18950) Docker: Version: 17.06.1-ce (Ubuntu VM) Java: version "1.8.0_144"

I'm not able to trick the system via @Nested classes either. As soon as I'm adding @SpringBootTest the DockerExtension "seems" to be disabled. Being a package private, I'm unable to explicitly apply it again.

FaustXVI commented 7 years ago

It seems that the current version of spring (4), is not working seamlessly with JUnit 5. That would explain why no JUnit 5 feature (like @Nested) is working when using the spring annotation. Using JUnit 5 over JUnit 4 seems to do the trick. Finally, junit5 is currently on RC3.

robert-malai commented 7 years ago

Got that.

Still, I'm (trying) to use Spring 5.0 - which is M3, together with JUnit5 which is RC3 - technically, they don't exists yet ... The reason I've chosen Spring 5.0 is to "provision" for the future and for the "native" support of JUnit5.

I guess that I'll give it another shoot after we have the RC for Spring Boot 2.0.

FaustXVI commented 6 years ago

Hi @robert-malai, how is it now ? Did you tried it again ?

tg4444 commented 6 years ago

Hi @FaustXVI,

Facing the same issue using Spring-boot 2.0.2 (junit-jupiter.version: 5.1.1, spring: 5.0.6).

Here's the Java setup:

@SpringBootTest
@ExtendWith({SpringExtension.class})
@TestInstance(Lifecycle.PER_CLASS)
@Transactional
@Docker(image = "mysql", 
        ports = @Port(exposed = 3336, inner = 3306), 
        newForEachCase = false, 
        environments = {
            @Environment(key = "MYSQL_ROOT_PASSWORD", value = "rootpass"),
            @Environment(key = "MYSQL_DATABASE", value = "testdb"),
            @Environment(key = "MYSQL_USER", value = "user"),
            @Environment(key = "MYSQL_PASSWORD", value = "pass"),
        }, 
        waitFor = @WaitFor("mysqld: ready for connections"))
public class TestController {

And here is a fragment of the pom.xml:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <scope>test</scope>
            <version>${junit-platform.version}</version>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-engine</artifactId>
            <scope>test</scope>
            <version>${junit-platform.version}</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.faustxvi</groupId>
            <artifactId>junit5-docker</artifactId>
            <version>1.0.0-RC5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

Any suggestions/workaround?