title: How to test Classes with @ConfigurationProperties and @Autowired
body: <p>I want to test small parts of the application that rely on properties loaded with <code>@Autowired</code> and <code>@ConfigurationProperties</code>. I am looking for a solution loading only the required properties and not always the whole <code>ApplicationContext</code>.
  Here as reduced example:</p>
  
  <pre><code>@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());
      }
  }
  </code></pre>
  
  <p>Configuration Class:</p>
  
  <pre><code>public class TestConfiguration {
      @Bean
      @ConfigurationProperties(prefix = "test")
      public TestSettings settings (){
          return new TestSettings();
      }
  }
  </code></pre>
  
  <p>Settings Class:</p>
  
  <pre><code>public class TestSettings {
      private String property;
  
      public String getProperty() {
          return property;
      }
  
      public void setProperty(String property) {
          this.property = property;
      }
  }
  </code></pre>
  
  <p>The properties file in the resource folder contains the entry:</p>
  
  <pre><code>test.property=TEST_PROPERTY
  </code></pre>
  
  <p>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?</p>
  
  <p><strong>edit:</strong> 
  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 <em>TestSettings</em>.</p>
  
  <p>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.</p>