我正在尝试从 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/