我目前正在研究这种代码.. 在测试课上我得到了类似的东西:
@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 配置文件启动应用程序?
这就是 Testcontainers 框架。通常,@ActiveProfiles 可以工作 在主代码中使用@Profile。
@ActiveProfiles
不属于Testcontainers库。它属于 spring-framework。
测试类中的
@ActiveProfiles
注释指示Spring在此类中运行测试时激活“测试”配置文件。这允许您为测试进行特定配置,这里有一些信息:(https://www.baeldung.com/spring-tests-override-properties#springProfile)。它不需要在主代码中存在具有相同值的@Profile
注释。
测试容器如何工作是一个不同的问题,需要与弹簧配置文件分开考虑。但您可以从阅读文档开始:https://testcontainers.com/getting-started