【发布时间】:2021-12-13 10:43:18
【问题描述】:
运行测试容器会导致初始化失败。 配置类:
@ActiveProfiles("test")
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ContextConfiguration(initializers = {Postgres.Initializer.class})
@SpringBootTest
@Transactional
@Sql("classpath: schema.sql")
public abstract class IntegrationTestConfig {
@Autowired
protected ApplicationContext applicationContext;
@BeforeAll
static void startContainer(){
Postgres.CONTAINER.start();
}
}
初始化类:
@UtilityClass
public class Postgres {
public static final PostgreSQLContainer CONTAINER = new PostgreSQLContainer("postgres:14.0");
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues.of(
"spring.datasource.url=" + CONTAINER.getJdbcUrl(),
"spring.datasource.username=" + CONTAINER.getUsername(),
"spring.datasource.password=" + CONTAINER.getPassword()
).applyTo(applicationContext);
}
}
}
- 为了简洁而省略了简单的测试类,我明白了:
Blockquoteork.test.context.support.AbstractContextLoader - 没有检测到测试类的默认资源位置 /**/.IntegrationTestConfig]:类路径资源 [//IntegrationTestConfigContext.groovy]不存在 11:08:22.594 [测试工作者] INFO org.springframework.test.context.support.AbstractContextLoader - 无法检测到测试类的默认资源位置 [*//.IntegrationTestConfig]:找不到后缀的资源 {-上下文.xml,上下文.groovy}。 11:08:22.596 [测试工作者] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - 无法检测到测试类 [/**/IntegrationTestConfig] 的默认配置类:IntegrationTestConfig 未声明任何静态、非-私有的、非最终的、用@Configuration 注释的嵌套类。 11:08:22.825 [测试工作者] 调试 org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - 已识别候选组件类:文件 [/home/user///Application.class] 11:08:22.827 [测试工作者] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - 找到 @SpringBootConfiguration *//Application for test class /**/RepositoryTest 11:08:22.983 [测试工作者] 调试 org.springframework.boot.test.context.SpringBootTestContextBootstrapper - 类 [//tRepositoryTest] 不存在 @TestExecutionListeners:使用默认值。 11:08:22.984 [测试工作者] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - 从位置 [META-INF/spring.factories] 加载默认 TestExecutionListener 类名:[org.springframework.boot.test.autoconfigure. restdocs.RestDocsTestExecutionListener,org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener,org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener,org.springframework.boot.test.autoconfigure.web.servlet。 WebDriverTestExecutionListener,org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener,org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener,org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener,org.springframework。 test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.spring framework.test.context.event.ApplicationEventsTestExecutionListener,org.springframework.test.context.support.DependencyInjectionTestExecutionListener,org.springframework.test.context.support.DirtiesContextTestExecutionListener,org.springframework.test.context.transaction.TransactionalTestExecutionListener,org.springframework。 test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener] 11:08:22.994 [测试工作者]调试 org.springframework.boot.test.context.SpringBootTestContextBootstrapper - 由于缺少依赖项而跳过候选 TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]。指定自定义侦听器类或使默认侦听器类及其所需的依赖项可用。违规类:[javax/servlet/ServletContext] 11:08:23.005 [测试工作者] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - 使用 TestExecutionListeners:[org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1f641fb7,org.springframework.test.context.event。 ApplicationEventsTestExecutionListener@69dc7b24, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@297454f7, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@602b7944, org.springframework.test.context.support.DirtiesContextTestExecutionListener@5f8d4b51, springframework.test.context.transaction.TransactionalTestExecutionListener@55821edf, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@5e5a2b74, org.springframework.test.context.event.EventPublishingTestExecutionListener@658098a2, org.springframework.boot.test.autoconfigure。 restdocs.RestDocsTestExecutionListener@50628080, org.springframework.boot.test.autoconfigure.web.client .MockRestServiceServerResetTestExecutionListener@155318b5, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@1add90e0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@7bf94e91, org.springframework.boot.test.autoconfigure .webservices.client.MockWebServiceServerTestExecutionListener@3fdbb7ee, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@32c08610] 11:08:23.010 [测试工作者]调试 org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - 测试类之前:上下文 [DefaultTestContext@11939a9f testClass = RepositoryTest, testInstance = [null], testMethod = [null], testException = [ null],mergedContextConfiguration = [ReactiveWebMergedContextConfiguration@229d508c testClass = RepositoryTest,位置 = '{}',类 = '{class *//Application}',contextInitializerClasses = '*/**/Postgres$Initializer]', activeProfiles = '{test}',propertySourceLocations = '{}',propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}',contextCustomizers = set[org.springframework.boot.test.autoconfigure.actuate .metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@1a10c47e, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@4816c0b, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverCo ntextCustomizerFactory$Customizer@33e50ff2, [ImportsContextCustomizer@5e83298e key = [org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration]], org.springframework.boot.test.context .filter.ExcludeFilterContextCustomizer@27261190, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@67d5ac2f, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client .TestRestTemplateContextCustomizer@6029f2a4, org.springframework.boot.test.web.reactive.server.WebTestClientContextCustomizer@5bb97fe7, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@7e19755a ], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map[[empty]]], 用@DirtiesContex注解的类t [false] 与模式 [null]。 11:08:23.105 [Test worker] DEBUG org.testcontainers.utility.TestcontainersConfiguration - Testcontainers 配置覆盖将从文件加载:/home/user/.testcontainers.properties 11:08:23.140 [Test worker] WARN org.testcontainers.utility.TestcontainersConfiguration - 尝试读取文件:/home/user/.testcontainers.properties 中的 Testcontainers 配置文件,但未找到该文件。异常消息:FileNotFoundException:/home/user/.testcontainers.properties(没有这样的文件或目录) 11:08:23.151 [测试工作者] INFO org.testcontainers.dockerclient.DockerMachineClientProviderStrategy - 在 PATH ([/app/bin, /usr/bin]) 上找不到 docker-machine 可执行文件 11:08:23.151 [测试工作者]调试 org.testcontainers.dockerclient.RootlessDockerClientProviderStrategy - $XDG_RUNTIME_DIR 已设置,但“/run/user/1000/docker.sock”不存在。 11:08:23.152 [测试工作者]调试 org.testcontainers.dockerclient.RootlessDockerClientProviderStrategy - '/home/user/.docker/run' 不存在。 11:08:23.220 [测试工作者] 调试 org.testcontainers.dockerclient.RootlessDockerClientProviderStrategy - '/run/user/1000/docker.sock' 不存在。 11:08:23.224 [测试工作者]调试 org.testcontainers.dockerclient.DockerClientProviderStrategy - UnixSocketClientProviderStrategy: 失败,异常 InvalidConfigurationException(找不到 unix 域套接字)。根本原因 NoSuchFileException (/var/run/docker.sock) 11:08:23.224 [测试工作者] 错误 org.testcontainers.dockerclient.DockerClientProviderStrategy - 找不到有效的 Docker 环境。请检查配置。尝试的配置是: 11:08:23.224 [测试工作者] 错误 org.testcontainers.dockerclient.DockerClientProviderStrategy - UnixSocketClientProviderStrategy: 失败,异常 InvalidConfigurationException(找不到 unix 域套接字)。根本原因 NoSuchFileException (/var/run/docker.sock) 11:08:23.224 [测试工作者] 错误 org.testcontainers.dockerclient.DockerClientProviderStrategy - 由于未找到有效配置,因此无法继续执行
我尝试切换 tc 版本: 1.16.2 \1,1.15.3 。我检查了用户权限 - 我已经启动了 docker w/o sudo。我还检查了 postgres 图像 - docker 和 tc 初始化程序中的 14.0。我还有什么办法可以解决这个问题?
【问题讨论】:
-
UPD chmod 777 /var/run/docker.sock 没有解决问题。 '' ''UnixSocketClientProviderStrategy: 因异常 InvalidConfigurationException 失败(找不到 unix 域套接字)。根本原因 NoSuchFileException (/var/run/docker.sock)''
标签: spring-boot docker testcontainers