gpt4 book ai didi

java - 在 Spring Boot 中跳过 VPN DNS 可用性的集成测试

转载 作者:行者123 更新时间:2023-11-30 05:35:23 24 4
gpt4 key购买 nike

我有一个中等重量的 springboot 服务,在正常流程上启动需要 10-15 秒,在重试/故障转移流程上需要 (1-2) 分钟失败。这对于我的业务流程来说是可以的,也是我期望健康服务的表现方式。

我有集成测试(在我的服务中运行一些端到端流程),只能在测试机器(或开发机器)连接到特定 VPN 时测试实际集成状态。

如果我未连接到 VPN,我想自动跳过集成测试。

考虑以下代码

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Server.class}, // auto scans a bunch of components
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) // slow loading context
public class IntegrationTest {
@BeforeClass
public static void beforeClass() {
Assume.assumeTrue(DnsTool.vpnConnected()); // fast failing test
}

@Test
public void testIntegration() {
// some test logic
}
}

当假设通过时,我的测试就会运行,一切都很好。当假设失败时,我的测试会被跳过,但只有在尝试加载昂贵的上下文之后才会跳过。

如何避免测试套件运行时间过长?

我尝试过的事情:

  • 子类化 SpringJUnit4ClassRunner,并重写 isTestMethodIgnored
  • 添加一个TestExecutionListener,并在beforeTestClass中抛出假设异常

这些对 Spring 没有任何影响,并且上下文以任何方式加载。

我没有尝试过的事情:

  • 我认为 Lazy init 是在 Spring 的下一个稳定版本 2.2.X 中附带的。

惰性初始化可能会让我的问题消失,但我觉得应该有一些我缺少的简单的 spring-test/junit 修复。

预先感谢您的帮助。

最佳答案

对我来说,这听起来像是你根本不应该在测试中做的事情。测试(至少恕我直言)应该检查业务案例并假设环境已设置并准备就绪。

也许值得将此功能委托(delegate)给构建工具和 CI。

示例:

在 Maven(或您使用的任何构建工具)中定义一个配置文件,该配置文件将运行需要 VPN 的集成测试。定义还将运行所有其余集成测试的配置文件。如果某些系统属性可用,则激活配置文件。

在 CI 工具(如 Jenkins)中,作为 CI 的一部分,甚至在运行 Maven 之前,运行将检查 VPN 连接的脚本。根据结果​​设置系统属性并使用这些属性运行 Maven。将加载所需的配置文件,并运行所有测试/仅不需要 VPN 的测试。

更新

如果你需要让它在 Spring 中工作(看起来你更喜欢这种方式),Spring有一个特殊的注解叫做@IfProfileValue

默认情况下,它与系统属性匹配,如果值不匹配,则测试将被忽略。

它看起来像这样(请注意,您也可以将此注释放在类上,然后它将适用于类中的所有测试方法):

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyTestClass {


@IfProfileValue(name = "os.name", values = {"Linux"})
@Test
public void testMe() {
// will run only in linux, otherwise
// won't even try to load an
// application context
....
}
}

这涵盖了您在外部解析 VPN 连接并使用属性运行测试时的情况。但是,如果您想在 java 中实现 VPN 连接检查,此注释还不够,因为它只能与 Java 系统属性一起使用,因此为了使用自定义逻辑,您需要实现 org.springframework.test.annotation.ProfileValueSource :

public class VPNConnectivityProfileValueSource implements ProfileValueSource {

private String vpnEnabled = "true";
public VPNConnectivityProfileValueSource () {
// no spring context is available here
ClassPathResource resource = new ClassPathResource("vpn-config.properties");

if (resource.exists()) {
// read the VPN address,
//
//this.testProps = PropertiesLoaderUtils.loadProperties(resource);
// invoke your utility, check the connectivity, etc.
this.vpnEnabled = ...
}

}

@Override
public String get(String key) {
// this is important method,
if(key.equals("vpn.enabled") {
return this.vpnEnabled;
}
else return System.getProperty(key);
}
}

最后一件事是让测试了解 ProfileValueSource:为此,您在测试中添加了另一个特殊注释: @ProfileValueSourceConfiguration(VPNConnectivityProfileValueSource.class)

总而言之,测试可能如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest
@ProfileValueSourceConfiguration(VPNConnectivityProfileValueSource.class)
@IfProfileValue(name = "vpn.enabled", value = "true")
public class MyTestClass {

@Test
public void testMe() {
....
}
}

我提到的所有类/注释都位于包 org.springframework.test.annotation

关于java - 在 Spring Boot 中跳过 VPN DNS 可用性的集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56756525/

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