micronaut-projects / micronaut-test-resources

Test resources support
Apache License 2.0
8 stars 17 forks source link

Cannot Open Connection to Docker From Gradle Test Suite #223

Open SenorPez opened 1 year ago

SenorPez commented 1 year ago

Expected Behavior

Connection should be made to Docker by Test Resources.

Actual Behaviour

When run from a Gradle Test Suite instead of the built-in test task, the test attempts to connect to 127.0.0.1:27017 instead of localhost:56559 (or another randomly generated port, depending on settings). This results in the following error and no further test progress as the Docker images aren't created or accessible.

com.mongodb.MongoSocketOpenException: Exception opening socket

Steps To Reproduce

Create a gradle build with an integration Test Suite.

testing {
    suites {
        withType(JvmTestSuite) {
            dependencies {
                implementation("org.assertj:assertj-core:3.24.2")

            }
        }
        test {
            useJUnitJupiter()
            dependencies {
                implementation('org.mockito:mockito-junit-jupiter:5.2.0')
                implementation("org.testcontainers:junit-jupiter")
                implementation("org.testcontainers:mongodb")
                implementation("org.testcontainers:testcontainers")
                implementation 'io.projectreactor:reactor-test:3.5.4'
            }
        }

        integrationTest(JvmTestSuite) {
            dependencies {
                implementation(project(project.path))

                implementation('com.networknt:json-schema-validator:1.0.78')
                implementation('io.micronaut.test:micronaut-test-junit5:3.9.2')
                implementation('io.micronaut:micronaut-http-client:3.8.7')
            }
        }
    }
}

Create a test that hits the database

    @Test
    void emptyDatabaseContainsNoDesigns() {
        HttpResponse<List<DesignModel>> response = designClient.get();
        assertThat(response.getStatus()).isEqualTo(HttpStatus.OK);
        assertThat(response.getBody().isPresent()).isTrue();
        assertThat(response.getBody().get().size()).isEqualTo(0);
    }

The exact same test will properly start, pass, and close the test resources database in the test source set, and fail in the integrationTest source set.

Environment Information

Example Application

No response

Version

3.8.8

melix commented 1 year ago

Sorry for the late answer but could you provide a reproducer? From your configuration it seems that you are not using test resources, since you are adding dependencies on testcontainers itself, which shouldn't be the case if you are using test resources.