SpringBootTest 不会使用 Spring Boot 2.4.3

问题描述

我有一个 @SpringBoottest 集成测试的问题,这让我发疯,我希望周围有一些 Spring 专家可以对这个问题有所了解:

我们的集成测试需要有一个完全初始化的 ApplicationContext,所以我们不会用任何特殊的测试配置文件来运行它们。这在所有环境中都运行良好。我刚刚更新到 Spring Boot 2.4.3(从 2.3.4 开始)并遇到了一个非常奇怪的问题:

当我在本地运行测试时(无论是从 IDE 还是使用 mvn test),它都按预期运行:因为我没有要运行的配置文件,所以他选择了认的 default 配置文件作为它启动时也显示在 INFO 日志输出中:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__,| / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.3)
2021-02-25 14:24:29.920  INFO [service-api] [           main] d.r.i.i.s.a.ServiceApiApplicationTest : No active profile set,falling back to default profiles: default
2021-02-25 14:24:31.599  INFO [service-api] [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
...

如果使用 mvn test 在我们的 CI 管道中运行同样的事情(实际上它是 Gitlab AutoDevOps,它利用了 herokuish java buildpack,但我很确定它归结为 mvn test)他会不要使用认的 default 配置文件,这会导致之后出现问题:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__,| / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.3)
2021-03-10 15:31:03.925  INFO [service-api] [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2021-03-10 15:31:04.034  INFO [service-api] [           main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
...

(你看到了问题:我们对每个环境都使用 Spring Cloud 配置除了 default 但这只是一个后续问题,根本原因是在 Gitlab CI 中运行时它只是不使用 default 配置文件。)

所以问题是:环境的哪一部分使得神奇的 Spring Boot Context 初始化过程没有通过标准的 org.springframework.boot.SpringApplication#logStartupProfileInfo 代码(就像它记录在 official documentation :

注解的工作原理是通过 SpringApplication 创建在您的测试中使用的 ApplicationContext。

此问题仅在更新到 Spring Boot 2.4.3 后出现,使用 v2.3.4,它在 Gitlab CI 中与本地一样好。

(哦,我知道我可以通过使用 @ActiveProfiles("default") 在测试中指定配置文件解决这个问题,但您不同意这很可怕吗?)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)