本文介绍了Spring Boot访问application.properties进行JUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Spring Boot应用程序内的JUnit内部访问application.properties或application-test.properties.

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

我不断看到在Stack Overflow上执行此操作的不同方法,只希望使用我的AppConfig类(见下文).

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

我正在使用Spring Boot 1.5.6 RELEASE和Java 1.8

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:


com.myservice.config.AppConfig:

@Configuration
public class AppConfig {

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

    // Getters & setters omitted for brevity
}

main/resources/application.properties:

main/resources/application.properties:

server.contextPath=/MyService
server.port=8080

# users dir
userDir=/opt/users

test.resources/application-test.properties

test.resources/application-test.properties

# users dir
userDir=/opt/users

能够获得&在主Spring Boot应用程序中解析此目录,但是当我尝试从JUnit测试类getAllUsers()测试方法中获取该目录时,我得到了NullPointerException:

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:

  • JUnit类可以从main/resources/application.properties中读取application.properties.
  • 使用this.class.getClassLoader.getResourceAsStream(...)//希望使用我的AppConfig.class
  • That JUnit classes can read application.properties from main/resources/application.properties.
  • Using this.class.getClassLoader.getResourceAsStream(...) // Wish to use my AppConfig.class

问题:

  1. 我可能在做错什么,应该从application.properties文件还是从test-properties.file读取?

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

如何配置@ActiveProfiles("test"),我应该在Maven pom.xml中的某个位置进行设置吗?

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

我是否需要将AppConfig放置在test/java/com/myservice/config下的源文件夹中?

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

推荐答案

只需注释 您上的@TestPropertySource("classpath:config/testing.properties") 测试班.

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

例如,您应该可以使用 @Value注释.

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

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

这篇关于Spring Boot访问application.properties进行JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 05:01