Issues with test container when running integration tests gradle task

474 Views Asked by At

I have this integration test base that test classes extend from, running one test class or a single test works without issues but if I try to run gradle task to run all tests I'm having this issue:

org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection

Connection to localhost:50234 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

@ActiveProfiles("itest")
@Testcontainers
@Import({DatabaseCleaner.class, IntegrationTestSetup.class})
@ContextConfiguration(initializers = IntegrationTestBase.ContextInitializer.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class IntegrationTestBase {

    @Autowired
    public DatabaseCleaner databaseCleaner;

    @BeforeEach
    public void globalBeforeEach() {
        databaseCleaner.truncate();
    }

    @Container
    static final PostgreSQLContainer postgresTestContainer = new PostgreSQLContainer("postgres:15")
            .withUsername("itest")
            .withPassword("itest")
            .withDatabaseName("catsit");

    public static class ContextInitializer implements
            ApplicationContextInitializer<ConfigurableApplicationContext> {

        @Override
        public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
            final String url = postgresTestContainer.getJdbcUrl();
            final String username = postgresTestContainer.getUsername();
            final String password = postgresTestContainer.getPassword();
            TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
                    configurableApplicationContext,
                    "spring.datasource.url=" + url,
                    "spring.datasource.username=" + username,
                    "spring.datasource.password=" + password,
                    "spring.liquibase.url=" + url,
                    "spring.liquibase.user=" + username,
                    "spring.liquibase.password=" + password,
                    "spring.liquibase.change-log=" + "classpath:db/changelog/master.xml"
            );
        }
    }

}
testing {
    suites {
        test {
            useJUnitJupiter()
        }

        itest(JvmTestSuite) {
            testType = TestSuiteType.INTEGRATION_TEST

            dependencies {
                implementation project()
            }

            configurations {
                itestImplementation.extendsFrom testImplementation
                itestRuntime.extendsFrom testRuntime
                itestRuntimeOnly.extendsFrom testRuntimeOnly
            }
        }
    }
}
0

There are 0 best solutions below