0

嗨,我有一个 Spring Boot 项目,我有一个集成测试。在我的集成测试中,我需要启动一个嵌入式弹性搜索实例。我有一个 application.yml 文件(src/test/resources/config/application.yml),我在其中定义弹性搜索配置,如下所示。

elasticsearch:
    configuration:
        clustername: my-cluster
        host: localhost
        port: 57457

所以我想要什么我需要在我的junit测试中获得这个值。但是我发现@Value 注释不适用于静态字段。任何想法应该如何正确地从 yml 文件中获取值。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = PrimecastApp.class)
public class PerformanceReportingIntTest extends AbstractCassandraTest {

    @Autowired
    private PerformanceReportingService performanceReportingService;

    @Autowired
    private HttpMessageConverter[] httpMessageConverters;

    @Autowired
    private ExceptionTranslator exceptionTranslator;

    @Value("${elasticsearch.configuration.host}")
    private final static String host = null;

    @Value("${elasticsearch.configuration.port}")
    private final static int port = 0;

    private static EmbeddedElastic embeddedElastic;

    private static RestHighLevelClient client;

    @BeforeClass
    public static void startElasticServer() throws FileNotFoundException, IOException, InterruptedException {


        embeddedElastic = EmbeddedElastic.builder().withElasticVersion("6.6.1")
                .withSetting(PopularProperties.HTTP_PORT, port)
                .withSetting(PopularProperties.CLUSTER_NAME, "my_cluster").withStartTimeout(5, TimeUnit.MINUTES)
                .withIndex("cars", IndexSettings.builder().withType("car", getSystemResourceAsStream()).build()).build()
                .start();

        client = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, "http")));


    }

感谢任何帮助谢谢

4

0 回答 0