TestContainers - disable test containers based on spring profile

84 Views Asked by At

I am new to test containers and working on writing integration test cases using spring boot and test containers.

I have to write multiple test cases and i should be able to switch between different environments to run these test cases. To achieve this, i am the following the below approach and it is working fine.

--to run the test cases locally

@SpringBootTest(classes=TestProjectApplication.class)
@ActiveProfiles("local")
public class IntegrationTestLocal {

    static {
        DockerComposeContainer container =
                new DockerComposeContainer(new File("/home/test/docker/mock.yml"))
                        .withExposedService("test-container-sybase", 5011);
        container.start();
    }
} 

--to run the test cases on dev

@SpringBootTest(classes= TestProjectApplication.class)
@ActiveProfiles("dev")
public class IntegrationTestDev {

}

But the problem is, all my test cases should extend any one of these class. All the test cases should not be dependent on the environment.

public class IntegrationTest extends IntegrationTestLocal/IntegrationTestDev {

    @Test
    void test(){
        
    }
}

So, is there anyway i can achieve this like getting the active profile inside static block and disable test containers if needed. Something like this, having one single base class and create containers only of the profile is local? or any other better approach?

@SpringBootTest(classes=TestProjectApplication.class)
public class IntegrationTest {

    @Value("${active.profile}")
    private static String activeProfile;

    static {
        if("local".equals(activeProfile)){
            DockerComposeContainer container =
                    new DockerComposeContainer(new File("/home/bharathip/test/docker/mock.yml"))
                            .withExposedService("test-container-sybase", 5011);
            container.start();
        }
    }
}
0

There are 0 best solutions below