Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在单元/集成测试阶段,将属性值注入/覆盖到Spring启动属性文件_Java_Unit Testing_Spring Boot_Integration Testing - Fatal编程技术网

Java 在单元/集成测试阶段,将属性值注入/覆盖到Spring启动属性文件

Java 在单元/集成测试阶段,将属性值注入/覆盖到Spring启动属性文件,java,unit-testing,spring-boot,integration-testing,Java,Unit Testing,Spring Boot,Integration Testing,使用SpringBoot,我需要一种方法来动态地告诉应用程序testcontainer正在侦听的端口是什么 我知道在测试期间,我可以告诉Spring使用不同的属性文件: @TestPropertySource(locations = "classpath:application-integrationtests.yml") 但是由于端口是随机的,我需要通过编程将值注入到Spring或属性文件中 我不是说@Value参数,因为它将从属性文件向bean注入一个值,因为当应用程序处于测试阶段时,无法

使用SpringBoot,我需要一种方法来动态地告诉应用程序testcontainer正在侦听的端口是什么

我知道在测试期间,我可以告诉Spring使用不同的属性文件:

@TestPropertySource(locations = "classpath:application-integrationtests.yml")
但是由于端口是随机的,我需要通过编程将值注入到Spring或属性文件中


我不是说
@Value
参数,因为它将从属性文件向bean注入一个值,因为当应用程序处于测试阶段时,无法知道该值是什么。

可能有更好的方法,但我只是使用系统属性来实现这一点

@SpringBootTest
@DirtiesContext
public class MyTest {
    @BeforeClass
    public static void setUpEnvironment() {
        System.setProperty("kafka.bootstrap.servers", testKafka.getServers(); 
    }
    ...
}

很难写出正确的答案,因为您没有显示使用Testcontainers的代码。但是:

类规则提供了发现测试如何与容器交互的方法:

getContainerIpAddress()
返回容器正在侦听的IP地址
getMappedPort(…)
返回容器上已公开的端口的Docker映射端口

例如,对于上面的Redis示例,以下内容将允许您的测试访问Redis服务:

String redisUrl=redis.getContainerIpAddress()+”:“+redis.getMappedPort(6379)


因此,您应该能够轻松地访问这些信息。

下面是@Dirk Deyne伟大的链接,我在这里添加了Testcontainer解决上述问题的方案副本(稍作修改):

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class,webEnvironment = 
                           WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = MyIntegrationTest.Initializer.class)
public class MyIntegrationTest {

public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        TestPropertyValues values = TestPropertyValues.of(
                "some.value.1=" + someObject.getSomeValue(),
                "some.value.2=" + someObject.getOtherValue()
        );
        values.applyTo(configurableApplicationContext);
    }
  }
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(类=DemoApplication.class,webEnvironment=
WebEnvironment.RANDOM_端口)
@ContextConfiguration(初始值设定项=MyIntegrationTest.Initializer.class)
公共类MyIntegrationTest{
公共静态类初始值设定项实现ApplicationContextInitializer{
@凌驾
公共无效初始化(ConfigurableApplicationContext ConfigurableApplicationContext){
TestPropertyValues values=TestPropertyValues.of(
“some.value.1=“+someObject.getSomeValue(),
“some.value.2=“+someObject.getOtherValue()
);
applyTo(configurableApplicationContext);
}
}
}

看看这里@Dirk Deyne你的链接正是我想要的。这帮我解决了问题。