gpt4 book ai didi

java - 在 Intellij 中使用测试 jar 进行 Spring Boot

转载 作者:行者123 更新时间:2023-11-30 07:09:53 26 4
gpt4 key购买 nike

您好,我有一个 Spring Boot 应用程序,可以从命令行正常运行。但是,当我从 ide (intellij) 运行它时,它在每个 http 请求上都会失败。我遇到的问题是,IDE 中的测试 jar(范围为 maven 中的测试)与我的应用程序中的 hk2 jar 冲突。

我无法从测试 jar 中排除 hk2 类,因为它是阴影的。

当然,在我的测试中也会发生这种情况。所以我的问题是如何保证 spring boot 仅加载生产 jar 而不是测试 jar。我担心我的测试可能没有测试正确的二进制文件。

进行执行已编译应用程序的集成测试是一种解决方案,但我想从 ide 运行它,而无需每次都重新编译代码。

测试 jar 是 testcontainers postgres 1.1.5

谢谢

最佳答案

如果你愿意信任testcontainers的hk2着色类,那么你可以定义2个maven配置文件,一个用于生产,一个用于测试,我称之为 hk2test生产配置文件(默认激活)包括hk2,而hk2test包括testcontainers。它们是相互排斥的。

定义 2 个 Maven 配置文件

<profiles>
<profile>
<id>production</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>

<dependencies>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2</artifactId>
</dependency>
</dependencies>

</profile>

<profile>
<id>unit test</id>
<activation>
<property>
<name>hk2test</name>
</property>
</activation>

<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>

在 Intellij 中激活测试配置文件

要使用 Intelj 的配置文件,请进入 Maven Project View 并选择 hk2test 配置文件。

从命令行激活测试配置文件

$ mvn -Dhk2test test

关于java - 在 Intellij 中使用测试 jar 进行 Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39403188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com