title: Spring Boot accessing application.properties for JUnit Test
body: <p>I am trying to access application.properties or application-test.properties from within JUnit inside my Spring Boot application.</p>
  
  <p>I keep seeing different ways to do this on Stack Overflow and only wish to use my AppConfig class (see below).</p>
  
  <p>Am using Spring Boot 1.5.6 RELEASE and Java 1.8</p>
  
  <hr>
  
  <p>Here's the structure to my application:</p>
  
  <pre><code>MyService
      pom.xml
      src
      ├───main
      │   ├───java
      │   │   └───com
      │   │       └───myservice
      │   │           ├───config
      │   │           │       AppConfig.java
      │   │           └───controller
      │   │           │       UserController.java
      │   │           │ 
      │   │           └─── MyServiceApplication.java    
      │   ├───resources
      │   │      application.properties
      └───test
          ├───java
          │     └───com
          │         └───myservice
          │               └───controller
          │                    UserControllerTest.java
          └───resources
                  application-test.properties    
  </code></pre>
  
  <hr>
  
  <p>com.myservice.config.AppConfig:</p>
  
  <pre><code>@Configuration
  public class AppConfig {
  
      @Value("${userDir}")
      private String userDir;
  
      // Getters &amp; setters omitted for brevity
  }
  </code></pre>
  
  <p>main/resources/application.properties:</p>
  
  <pre><code>server.contextPath=/MyService
  server.port=8080
  
  # users dir
  userDir=/opt/users
  </code></pre>
  
  <p>test.resources/application-test.properties</p>
  
  <pre><code># users dir
  userDir=/opt/users
  </code></pre>
  
  <p>Am able to obtain &amp; parse this dir within my main Spring Boot application, but I get a NullPointerException when I tried to get it from within my JUnit test class getAllUsers() test method:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @WebMvcTest(UserController.class)
  @ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)
  @ActiveProfiles("test")
  public class UserControllerTest {
  
      @Autowired
      MockMvc mvc;
  
      @MockBean
      AppConfig config;
  
      @MockBean
      UserService userService;
  
      @Test
      public void getAllUsers() throws Exception {
          User users = new User(config.getUserDir());
  
          // more lines of code
      }
  }
  </code></pre>
  
  <hr>
  
  <p>On Stack Overflow there are multiple ways to do this which I read as:</p>
  
  <ul>
  <li>That JUnit classes can read application.properties from main/resources/application.properties.</li>
  <li>Using <code>this.class.getClassLoader.getResourceAsStream(...)</code> // Wish to use my AppConfig.class</li>
  </ul>
  
  <p>Question(s):</p>
  
  <ol>
  <li><p>What am I possibly doing wrong, should it just read from the application.properties file or from the test-properties.file?</p></li>
  <li><p>How does one configure the @ActiveProfiles("test"), am I supposed to set this inside the Maven pom.xml somewhere?</p></li>
  <li><p>Do I need to place AppConfig inside a source folder under test/java/com/myservice/config?</p></li>
  </ol>