spring-boot - 来自 .properties 文件的 Spring Boot 单元测试 @Value 给出 NullPointerException

标签 spring-boot junit spring-boot-test spring-junit

我正在尝试从 Spring Boot 中的单元测试用例的属性文件中读取值。我有两个 config.properties文件,一个在 src/main/resources :

prop = some-value

和一个在 src/test/resources :

prop = some-test-value

主要应用类:

package company.division.project;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.PropertySource;

@SpringBootApplication(scanBasePackages = "company.division.project")
@PropertySource(value = "classpath:config.properties")
public class Application extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        System.setProperty("DUMMY_PROPERTY", "dummy-value");

        return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
        // Do nothing with main
    }
}

要测试的服务类:

package company.division.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class Service {
    @Autowired
    Environment environment;

    public String getProperty() {
        return environment.getProperty("prop");
    }

}

服务测试类。我尝试了两种方法来检索 src/test/resources/config.properties 中的值。文件;一个带有 @Autowired Environment ,还有一个带有 @Value注释...都没有奏效:

package company.division.project.service;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.test.context.TestPropertySource;

@RunWith(MockitoJUnitRunner.class)
@TestPropertySource("classpath:config.properties")
public class ServiceTest {
    @InjectMocks
    Service service;

    @Autowired
    Environment environment;

    @Value("${prop}")
    private String expectedProperty;

    @Test
    public void testGetPropertyWithValueAnnotation() {
        assertEquals(expectedProperty, service.getProperty());
    }

    @Test
    public void testGetPropertyWithEnvironment() {
        assertEquals(environment.getProperty("prop"), service.getProperty());
    }
}

我在 StackOverflow 上的某个地方读到,为了在 Spring 测试类中自动连接组件,我需要为测试创建一个完整的上下文,所以我尝试了这个(更改注释和测试运行程序):

package company.division.project.service;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceTest {
    @InjectMocks
    Service service;

    @Autowired
    Environment environment;

    @Value("${prop}")
    private String expectedProperty;

    @Test
    public void testGetPropertyWithValueAnnotation() {
        assertEquals(expectedProperty, service.getProperty());
    }

    @Test
    public void testGetPropertyWithEnvironment() {
        assertEquals(environment.getProperty("prop"), service.getProperty());
    }
}

上下文已创建,但两种方法都以 NullPointerException 结束再一次。

最佳答案

您测试的问题在于您试图使用 MockitoJUnitRunner.class以错误的方式。

如果您正在使用 @InjectMocks 模拟服务你需要确保你需要返回值 Service.getProperty()通过模拟服务调用。如果您正在使用 SpringRunner.class那么你不应该有 @InjectMocks但应该有 @Autowired为服务。以下测试有效。

@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceTest {
    @Autowired
    Service service;

    @Autowired
    Environment environment;

    @Value("${prop}")
    private String expectedProperty;

    @Test
    public void testGetPropertyWithValueAnnotation() {
        assertEquals(expectedProperty, service.getProperty());
    }

    @Test
    public void testGetPropertyWithEnvironment() {
        assertEquals(environment.getProperty("prop"), service.getProperty());
    }
}

关于spring-boot - 来自 .properties 文件的 Spring Boot 单元测试 @Value 给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58062615/

相关文章:

java - eclemma 2 个分支中的 1 个未在 Junit 中覆盖

java - 如何在使用 @RunWith(SpringJUnit4ClassRunner.class) 执行任何测试之前 Autowiring Junit 类中的属性

spring - 如何以编程方式为任何环境设置事件配置文件?

java - 将 SpringRunner 与 PowermockRunner 结合使用时出现异常

mongodb - mongodb不存在时如何创建db?

java - 文件未找到异常 : for properties file in Spring Boot application deployed in Tomcat

spring - JSF 注释不适用于 Spring-boot

Java ModelMapper 在对象中映射对象

java - @在HSQL数据库中生成

spring-boot - @JdbcTest 检测带有@SpringBootApplication 注释的类