Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the run time properties on SpringApplicationBuilder()

I need to test my Spring application code by creating a Cucumber integration test. I am using SpringApplicationBuilder to start up my application before the actual logic is triggered and am using the following syntax to do so:-

    application = new SpringApplicationBuilder()
        .parent(new Object[]{"classpath:file1.xml", "classpath:file2.xml"})
        .profiles("abc")
        .properties("name:value") [It has 5/6 (name:value) pairs here]*
        .showBanner(false)
        .logStartupInfo(true)
        .headless(true)
        .application()
        .run();

My Spring application starts up correctly. However, it does not get the values for the property (name, value) pairs that I am passing to the SpringApplicationBuilder(). I have tried the following to set them :-

  • Using name value pairs as above
  • Using a HashMap of (name, value) pairs
  • Creating a ConfigurableEnvironment, retrieving the MutablePropertySources and setting my properties in it.

None of these options are working, so when the application starts up and the code tries to access certain System Property values, it breaks.

Any ideas how this could be fixed.. All the help is greatly appreciated!

like image 461
Hello Avatar asked Jul 31 '26 04:07

Hello


1 Answers

The properties you configure on SpringApplicationBuilder are made available in your application's Environment, not as system properties. If your code's expecting system properties then you should set them using System.setProperty(key, value) or as -D arguments when you launch the JVM

like image 73
Andy Wilkinson Avatar answered Aug 01 '26 19:08

Andy Wilkinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!