I want to test small parts of the application that rely on properties loaded with @Autowired
and @ConfigurationProperties
. I am looking for a solution loading only the required properties and not always the whole ApplicationContext
. Here as reduced example:
@TestPropertySource(locations = "/SettingsTest.properties") @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {TestSettings.class, TestConfiguration.class}) public class SettingsTest { @Autowired TestConfiguration config; @Test public void testConfig(){ Assert.assertEquals("TEST_PROPERTY", config.settings().getProperty()); } }
Configuration Class:
public class TestConfiguration { @Bean @ConfigurationProperties(prefix = "test") public TestSettings settings (){ return new TestSettings(); } }
Settings Class:
public class TestSettings { private String property; public String getProperty() { return property; } public void setProperty(String property) { this.property = property; } }
The properties file in the resource folder contains the entry:
test.property=TEST_PROPERTY
In my current setup config is not null, but no fields are available. The reason the fields are not field should have something to do with the fact that I am not using Springboot but Spring. So what would be the Springboot way to get this running?
edit: The reason why I want to do this is: I have a parser that parses Textfiles, the regular expressions used are stored in a properties file. To test this I would like to load only the properties needed for this parser which are in the exaple above the TestSettings.
While reading the comments I already noticed that this are no Unit tests anymore. However using the full Spring boot configuration for this small test seems a bit too much to me. That's why I asked if there is a posibilty to load only the one class with properties.
Basically, you: use a specific configuration to @EnableConfigurationProperties and @EnableAutoConfiguration , listing all the @ConfigurationProperties files you want to load. in the test class, you load this configuration file of tests, with an initializer class defined by Spring to load application. yml file.
if you are writing unit tests a recommend you use @Mock and @InjectMocks . But if you really want test all the flow and need to inject classes, you can @RunWith(SpringJUnit4ClassRunner. class) and @Autowired your classes. Hello pedroso.
You need to annotate your TestConfiguraion with @EnableConfigurationProperties
as follows:
@EnableConfigurationProperties public class TestConfiguration { @Bean @ConfigurationProperties(prefix = "test") public TestSettings settings (){ return new TestSettings(); } }
Also you only need to include TestConfiguration.class
in @ContextConfiguration
of you SettingsTest
class:
@TestPropertySource(locations = "/SettingsTest.properties") @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestConfiguration.class) public class SettingsTest { ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With