确保在pom.xml文件中使用了正确的testcontainer依赖项。
为testcontainer添加一个DockerComposeContainer并将其添加到BitBucket管道脚本中。以下是示例代码:
@Testcontainers
public class SampleIntegrationTest {
private static final int SERVICE_PORT = 12345;
@Container
private static DockerComposeContainer container = new DockerComposeContainer(
new File("src/test/resources/docker-compose.yml"))
.withExposedService("test-container-service", SERVICE_PORT);
@Test
public void exampleTest() throws Exception {
URL serviceUrl = new URL("http://"
+ container.getServiceHost("test-container-service", SERVICE_PORT)
+ ":" + container.getServicePort("test-container-service", SERVICE_PORT));
// test the service using the URL above
}
}
将BitBucket管道脚本中的配置更改为使用docker-compose文件来运行测试。以下是一个示例:
pipelines:
branches:
master:
- step:
script:
- export SERVICE_URL=http://test-container-service:12345
- docker-compose -f docker-compose-test.yml up --abort-on-container-exit
为保持一致性,要确保在BitBucket中使用的所有Docker映像都已经初始化并完成构建。