我是测试容器的新手,所以我有一个问题。我在Spring/Hibernate上有一个应用程序。我有一个docker-image(h2 testbase)和一个带数据的mysql-base(myTestDb)。我用-p 6161:3306在docker中运行这个映像。在test/resources目录中,我有一个文件application.properties。它包含了下一个
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:6161/myTestDb?allowPublicKeyRetrieval=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Europe/Moscow&&useSSL=false
jdbc.username=root
jdbc.cred=admin
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
我使用了mvn测试-它工作正常。现在我想用Testcontainers运行这些测试。我添加了pom.xml依赖项
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version>1.9.1</version>
<scope>test</scope>
</dependency>
我扩展了MySQLContainer类
public class TestMySQL extends MySQLContainer {
public TestMySQL() {
super();
}
public TestMySQL(String dockerImageName) {
super(dockerImageName);
}
@Override
public String getDriverClassName() {
return "com.mysql.cj.jdbc.Driver";
}
}
因为MySQLContainer使用com.mysql.jdbc.Driver,它已经过时了。我的测试(例如)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {
HibernateConfiguration.class,
SecurityConfiguration.class,
SecurityInitializer.class,
ViewConfiguration.class,
ViewInitializer.class})
@WebAppConfiguration
public class ControllerServiceJTest {
@ClassRule
public static TestMySQL container
= new TestMySQL("h2testbase");
@Autowired
ControllerService controllerService;
@Test
public void stationPagination() {
Map<String, Object> pag = controllerService.stationPagination(4);
Assert.assertTrue(((List<Station>)pag.get("stations")).size() == 8);
}
@Test
public void trainPagination() {
Map<String, Object> pag = controllerService.trainPagination(1);
Assert.assertTrue(((List<Train>)pag.get("trains")).size() == 20);
}
@Test
public void switchHelper() {
Assert.assertTrue(controllerService.stationSwitchHelper("BLUE").equals(URLs.REDIRECT_DASHSTATION + "/2"));
}
}
如果我使用mvn测试,我会看到(通过docker ps)container启动了,它启动了两到三次(Map在随机端口上进行,如328 xx),但是maven告诉
org.testcontainers.containers.ContainerLaunchException: Container startup failed
Caused by: org.rnorth.ducttape.RetryCountExceededException: Retry limit hit with exception
Caused by: org.testcontainers.containers.ContainerLaunchException: Could not create/start container
Caused by: org.rnorth.ducttape.TimeoutException: org.rnorth.ducttape.TimeoutException: java.util.concurrent.TimeoutException
Caused by: org.rnorth.ducttape.TimeoutException: java.util.concurrent.TimeoutException
Caused by: java.util.concurrent.TimeoutException
我现在该怎么办?如何告诉我的testcontainer需要端口(6161)?如何使用参数,在www.example.com中application.properties?我找不到代码示例,其中使用了自定义图像与数据库。提前感谢
更新添加失败测试的完整结果。
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running ru.javasch.metro.junit.ControllerServiceJTest
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
SLF4J: Failed to load class "org.slf4j.impl.StaticMDCBinder".
SLF4J: Defaulting to no-operation MDCAdapter implementation.
SLF4J: See http://www.slf4j.org/codes.html#no_static_mdc_binder for further details.
?? Checking the system...
? Docker version should be at least 1.6.0
? Docker environment should have more than 2GB free disk space
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 97.189 s <<< FAILURE! - in ru.javasch.metro.junit.ControllerServiceJTest
[ERROR] ru.javasch.metro.junit.ControllerServiceJTest Time elapsed: 97.187 s <<< ERROR!
org.testcontainers.containers.ContainerLaunchException: Container startup failed
Caused by: org.rnorth.ducttape.RetryCountExceededException: Retry limit hit with exception
Caused by: org.testcontainers.containers.ContainerLaunchException: Could not create/start container
Caused by: org.rnorth.ducttape.TimeoutException: org.rnorth.ducttape.TimeoutException: java.util.concurrent.TimeoutException
Caused by: org.rnorth.ducttape.TimeoutException: java.util.concurrent.TimeoutException
Caused by: java.util.concurrent.TimeoutException
有一些信息。我尝试测试MySqlContainer从here(与我的TestMySql)。当我使用干净的mysql:5.5图像-所有的好。但当我尝试添加一些容器的修改(例如addFixedExposedPort)它不是开始,因为端口已经分配。在情况下,当我从脚本添加数据-它是“无法创建容器”。如果我试图给它我的图像(h2 testbase),再次“无法创建容器”。
2条答案
按热度按时间k10s72fa1#
看来你有两个问题。
addFixedExposedPort
设置固定端口1.您可能没有数据库
test
,用户test
,密码test
作为MySQLContainer中的默认凭据,这导致您得到ContainerLaunchException
。使用withDatabaseName
、withUsername
和withPassword
正确配置数据库和用户。更新:
若要打开日志记录,请将以下依赖项添加到
pom.xml
并使用以下内容创建
src/test/resources/logback.xml
fnatzsnv2#
我也遇到了同样的问题。当从MySQLContainer扩展一个自定义类时,容器似乎启动了多次。在我的例子中,启动了三个mysql容器。当我直接使用MySQLContainer类时,一切都按预期运行。只启动了一个mysql容器。