类中使用Testcontainers@ActiveProfiles,但主代码中没有@Profile

问题描述 投票:0回答:1

我目前正在研究这种代码.. 在测试课上我得到了类似的东西:

@ActiveProfiles("test")
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = TestcontainersApplication.class, webEnvironment = 
SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ProductRepositoryTest extends ContainersEnvironment {

@Autowired
private ProductRepository productRepository;

这就是 Testcontainers 框架。 通常,@ActiveProfiles 在主代码中与 @Profile 一起使用。

例如:

main code ==> @Profile("QA")
test package ==> @ActiveProfiles("QA")

我的问题是: 为什么在我的项目中,我没有任何 @Profile("test") 注释? 测试容器如何工作? 它是否使 Junit 测试使用 spring 配置文件启动应用程序?

spring profile testcontainers
1个回答
0
投票

这就是 Testcontainers 框架。通常,@ActiveProfiles 可以工作 在主代码中使用@Profile。

  1. @ActiveProfiles
    不属于Testcontainers库。它属于 spring-framework。

  2. 测试类中的

    @ActiveProfiles
    注释指示Spring在此类中运行测试时激活“测试”配置文件。这允许您为测试进行特定配置,这里有一些信息:(https://www.baeldung.com/spring-tests-override-properties#springProfile)。它需要在主代码中存在具有相同值的
    @Profile
    注释。

  3. 测试容器如何工作是一个不同的问题,需要与弹簧配置文件分开考虑。但您可以从阅读文档开始:https://testcontainers.com/getting-started

© www.soinside.com 2019 - 2024. All rights reserved.