5

I am trying to access application.properties or application-test.properties from within JUnit inside my Spring Boot application.

I keep seeing different ways to do this on Stack Overflow and only wish to use my AppConfig class (see below).

Am using Spring Boot 1.5.6 RELEASE and Java 1.8


Here's the structure to my application:

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    

com.myservice.config.AppConfig:

@Configuration
public class AppConfig {

    @Value("${userDir}")
    private String userDir;

    // Getters & setters omitted for brevity
}

main/resources/application.properties:

server.contextPath=/MyService
server.port=8080

# users dir
userDir=/opt/users

test.resources/application-test.properties

# users dir
userDir=/opt/users

Am able to obtain & 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:

@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
    }
}

On Stack Overflow there are multiple ways to do this which I read as:

  • That JUnit classes can read application.properties from main/resources/application.properties.
  • Using this.class.getClassLoader.getResourceAsStream(...) // Wish to use my AppConfig.class

Question(s):

  1. What am I possibly doing wrong, should it just read from the application.properties file or from the test-properties.file?

  2. How does one configure the @ActiveProfiles("test"), am I supposed to set this inside the Maven pom.xml somewhere?

  3. Do I need to place AppConfig inside a source folder under test/java/com/myservice/config?

halfer
  • 19,824
  • 17
  • 99
  • 186
PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144
  • 2
    Your test class defines `@MockBean AppConfig config;` so it is using a mock AppConfig instead of a real AppConfig that loads the application properties. – Chin Huang Apr 01 '19 at 22:31
  • Please refrain from asking for people to vote, or not vote, in a certain way. It doesn't help, and if you draw attention to something that should not be voted on, people may look more closely and do it anyway. In any case, we want voting to be organic here. – halfer Apr 02 '19 at 18:40
  • Did you find a solution for this? – rsp Jun 18 '21 at 15:30

2 Answers2

2

You can use @TestPropertySource annotation in your test class.

Just annotate @TestPropertySource("classpath:config/testing.properties") on your test class.

You should be able to read out the property for example with the @Value annotation.

@Value("${fromTest}")
private String fromTest;

your config like this src/main/resources/config/testing.properties

kumar
  • 497
  • 4
  • 12
0

Add the following annotation to your test class:

@TestPropertySource(locations="classpath:application-test.properties")

this should get you working :) please note that you have to import it:

 import org.springframework.test.context.TestPropertySource;
Hasson
  • 1,894
  • 1
  • 21
  • 25
  • @TestPropertySource(properties={"spring.config.location="}) will prevent the application.properties to be loaded along with answer by Hasson – user2758406 Feb 14 '22 at 16:19