TL;DR
使用Junit Jupiter测试的Java Sping Boot 在启动时抛出:
- java.lang.初始化程序中出现异常错误
- 原因:java.lang.运行时异常
- 异常错误:
- 等待日志输出匹配'.(数据库系统已准备好接受连接).'超时
MySQL已正确启动,但testcontainers无法解析匹配的字符串。
如何更改检测?
设置
技术堆栈
- java :21个
- Spring Boot :3.0.x
- 试验容器:1.19.x
- MySQL:8.2.x版本
编写
docker-compose.yml:
version: "3"
services:
# MySQL
database:
image: mysql:8.2
environment:
MYSQL_ROOT_PASSWORD: sampleRootPassword
MYSQL_DATABASE: sampledatabase
MYSQL_USER: identity
MYSQL_PASSWORD: identityPassword
expose:
- 3306
ports:
- "3306:3306"
restart: unless-stopped
volumes:
- ./config/mysql:/docker-entrypoint-initdb.d
networks:
- services_default
volumes:
mysql-data:
networks:
services_default:
字符串
简单的“SpringBoot应用程序测试”
package de.whatever.endpoint;
import de.whatever.testcontainers.DockerCompose;
import de.whatever.testcontainers.Service;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.http.HttpMethod.POST;
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = "spring.profiles.active=integrationtest"
)
@Testcontainers
@ContextConfiguration(initializers = {EndpointApplicationTest.Initializer.class})
class EndpointApplicationTest {
private static final Logger logger =
LoggerFactory.getLogger(EndpointApplicationTest.class);
public static DockerComposeContainer<?> dcEnv;
@Test
public void SampleTest() {
logger.info("Hello World");
int random = (int)(Math.random()*1000);
assertTrue(random > -1);
assertTrue(random < 1001);
}
static {
dcEnv = DockerCompose.createEnvironment(logger);
dcEnv.start();
}
static class Initializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
int port = DockerCompose.getServicePort(dcEnv, Service.DATABASE);
logger.info(String.format("--- MySQL[%s] ---", port));
String jdbc = String.format(
"jdbc:mysql://localhost:%s/identity?allowPublicKeyRetrieval=true¤tSchema=sampledatabase",
port
);
TestPropertyValues.of(
"spring.datasource.url=" + jdbc
).applyTo(configurableApplicationContext.getEnvironment());
}
}
}
型
正在运行
控制台输出(摘录):
... .. .
09:55:37.577 [docker-java-stream-138462075] INFO de.slr.kbaas.identityservice.authendpoint.AuthEndpointApplicationTest - STDERR: 2023-12-13T08:55:37.576080Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
09:55:37.578 [ducttape-0] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDERR: 2023-12-13T08:55:37.576080Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
09:55:37.695 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-1 << "117[\r][\n]"
09:55:37.695 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-1 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0]c2023-12-13T08:55:37.694592Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.[\n]"
09:55:37.695 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-1 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0][0xffffffa4]2023-12-13T08:55:37.694612Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.[\n]"
09:55:37.695 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-1 << "[\r][\n]"
09:55:37.695 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-2 << "117[\r][\n]"
09:55:37.695 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-2 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0]c2023-12-13T08:55:37.694592Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.[\n]"
09:55:37.695 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-2 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0][0xffffffa4]2023-12-13T08:55:37.694612Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.[\n]"
09:55:37.695 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-2 << "[\r][\n]"
09:55:37.695 [docker-java-stream-138462075] INFO de.slr.kbaas.identityservice.authendpoint.AuthEndpointApplicationTest - STDERR: 2023-12-13T08:55:37.694592Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
09:55:37.695 [docker-java-stream-138462075] INFO de.slr.kbaas.identityservice.authendpoint.AuthEndpointApplicationTest - STDERR: 2023-12-13T08:55:37.694612Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
09:55:37.695 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-2 << "db[\r][\n]"
09:55:37.695 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-1 << "db[\r][\n]"
09:55:37.695 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-2 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0][0xffffffd3]2023-12-13T08:55:37.695474Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.[\n]"
09:55:37.695 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-1 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0][0xffffffd3]2023-12-13T08:55:37.695474Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.[\n]"
09:55:37.695 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-2 << "[\r][\n]"
09:55:37.695 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-1 << "[\r][\n]"
09:55:37.695 [docker-java-stream-138462075] INFO de.slr.kbaas.identityservice.authendpoint.AuthEndpointApplicationTest - STDERR: 2023-12-13T08:55:37.695474Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
09:55:37.701 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-2 << "178[\r][\n]"
09:55:37.701 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-1 << "178[\r][\n]"
09:55:37.702 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-2 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0][0xffffffa0]2023-12-13T08:55:37.701605Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock[\n]"
09:55:37.702 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-1 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0][0xffffffa0]2023-12-13T08:55:37.701605Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock[\n]"
09:55:37.702 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-2 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0][0xffffffc8]2023-12-13T08:55:37.701652Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.2.0' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.[\n]"
09:55:37.702 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-1 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0][0xffffffc8]2023-12-13T08:55:37.701652Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.2.0' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.[\n]"
09:55:37.702 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-2 << "[\r][\n]"
09:55:37.702 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-1 << "[\r][\n]"
09:55:37.702 [docker-java-stream-138462075] INFO de.slr.kbaas.identityservice.authendpoint.AuthEndpointApplicationTest - STDERR: 2023-12-13T08:55:37.701605Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
09:55:37.702 [docker-java-stream-138462075] INFO de.slr.kbaas.identityservice.authendpoint.AuthEndpointApplicationTest - STDERR: 2023-12-13T08:55:37.701652Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.2.0' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
09:55:37.703 [ducttape-0] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDERR: 2023-12-13T08:55:37.701652Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.2.0' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
09:55:37.703 [ducttape-0] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDERR: 2023-12-13T08:55:37.701605Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
09:55:37.703 [ducttape-0] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDERR: 2023-12-13T08:55:37.695474Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
09:55:37.703 [ducttape-0] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDERR: 2023-12-13T08:55:37.694612Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
09:55:37.703 [ducttape-0] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDERR: 2023-12-13T08:55:37.694592Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
09:56:34.703 [ducttape-0] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.classic.InternalHttpClient - ep-0000001C: cancel
09:56:34.704 [ducttape-0] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.io.DefaultManagedHttpClientConnection - http-outgoing-1: close connection IMMEDIATE
09:56:34.704 [docker-java-stream-138462075] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire - http-outgoing-1 << "[read] I/O error: null"
09:56:34.704 [ducttape-0] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.classic.InternalHttpClient - ep-0000001C: endpoint closed
09:56:34.704 [ducttape-0] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.classic.InternalHttpClient - ep-0000001C: discarding endpoint
09:56:34.704 [ducttape-0] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager - ep-0000001C: releasing endpoint
09:56:34.704 [ducttape-0] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager - ep-0000001C: connection is not kept alive
. .. ...
型
错误
从一开始,Testcontainers就试图检测MySQL数据库的终结,这发生在09:55:37.703
[Server] /usr/sbin/mysqld: ready for connections. Version: '8.2.0' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
型
在09:55:37.703和09:56:34.703之间,未创建其他日志。
最后启动失败:
原因:java.lang.RuntimeException:ContainerLaunchException:等待日志输出匹配'.(数据库系统准备接受连接).'时超时
日志滚动
我假设testcontainers只是使用了错误的日志字符串匹配。遗憾的是,我不知道如何更改字符串。
此外,相同的对数线:
[服务器] /usr/sbin/mysqld:已准备好连接。版本:“8.2.0”套接字:“/var/run/mysqld/mysqld.sock”端口:3306 MySQL社区服务器- GPL。
...在09:55:37.702
中多次给定。
这同样适用于以下日志行:
[Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
型
我想这两种都不会造成损坏,如果他们的应用。但端口需要调整整个,因为日志行是在使用port 0
之前给出的。
最后的想法
- 是否有机会调整与
wait
匹配的测试容器字符串?
同样地:
- 如何更改检测?
1条答案
按热度按时间iqxoj9l91#
你需要的东西
docker-compose.yml
就像“在问题中”一样。
重复
字符串
设置Java类
型