0

Hi I am having a spring boot project and I have an integration test. In my integration test i need to start an embedded elastic search instance. I have an application.yml file (src/test/resources/config/application.yml) where i define the elastic search configuration like as follow.

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

so what i want i need to obtain this values in my junit test. however i found that @Value annotations dont work over static fields . Any idea how should in order to get the values from the yml file correctly.

@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")));


    }

Appreciate any help thank you

knowledgeseeker
  • 339
  • 4
  • 14
  • You don't. You should do it the other way around. Start the container and publish the host and port so that afterwards you can use that to configure the application. – M. Deinum Aug 26 '19 at 10:29
  • could you please elaborate a bit more . thank you – knowledgeseeker Aug 26 '19 at 10:56
  • Start the server on a random port and expose that to Spring Boot before it starts. Something like https://vanwilgenburg.wordpress.com/2019/01/22/embedded-elasticsearch-junit5-spring-boot/ – M. Deinum Aug 26 '19 at 11:25

0 Answers0