Java Spring jUnit测试属性文件
我有一个jUnit测试,它有自己的属性文件(application Test.properties)和spring配置文件(application core Test.xml) 其中一个方法使用spring配置实例化的对象,即spring组件。类中的一个成员从主属性文件application.properties派生其值。通过jUnit访问此值时,它始终为null。我甚至尝试更改属性文件以指向实际的属性文件,但这似乎不起作用 下面是我访问属性文件对象的方式Java Spring jUnit测试属性文件,java,spring,junit,spring-junit,Java,Spring,Junit,Spring Junit,我有一个jUnit测试,它有自己的属性文件(application Test.properties)和spring配置文件(application core Test.xml) 其中一个方法使用spring配置实例化的对象,即spring组件。类中的一个成员从主属性文件application.properties派生其值。通过jUnit访问此值时,它始终为null。我甚至尝试更改属性文件以指向实际的属性文件,但这似乎不起作用 下面是我访问属性文件对象的方式 @Component @Propert
@Component
@PropertySource("classpath:application.properties")
public abstract class A {
@Value("${test.value}")
public String value;
public A(){
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
public A(String text) {
this();
// do something with text and value.. here is where I run into NPE
}
}
public class B extends A {
//addtnl code
private B() {
}
private B(String text) {
super(text)
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:META-INF/spring/application-core-test.xml",
"classpath:META-INF/spring/application-schedule-test.xml"})
@PropertySource("classpath:application-test.properties")
public class TestD {
@Value("${value.works}")
public String valueWorks;
@Test
public void testBlah() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
B b= new B("blah");
//...addtnl code
}
}
首先,@PropertySource中的application.properties应该是
application test.properties
,如果这是文件的名称(匹配这些内容很重要):
该文件应位于/src/test/resources
类路径下(根目录下)
我不明白为什么要将依赖项硬编码到名为application test.properties
的文件中。该组件是否仅在测试环境中使用
通常要做的事情是在不同的类路径上具有相同名称的属性文件。根据是否正在运行测试,可以加载一个或另一个
在通常布局的应用程序中,您将有:
src/test/resources/application.properties
及
然后像这样注入它:
@PropertySource("classpath:application.properties")
更好的做法是在spring上下文中将该属性文件作为bean公开,然后将该bean注入任何需要它的组件中。这样,您的代码就不会到处都是对application.properties的引用,您可以使用任何您想要的内容作为属性源。下面是一个示例:对于测试,您应该使用Spring 4.1,它将覆盖在其他地方定义的属性:
@TestPropertySource("classpath:application-test.properties")
测试属性源的优先级高于从操作系统环境或Java系统属性加载的属性源,以及由应用程序添加的属性源,如@PropertySource我也面临同样的问题,在决定阅读文件之前,我花了太多的卡路里搜索正确的修复程序:
Properties configProps = new Properties();
InputStream iStream = new ClassPathResource("myapp-test.properties").getInputStream();
InputStream iStream = getConfigFile();
configProps.load(iStream);
如何实例化
A
的实例?我猜您使用的是new
,而不是从ApplicationContext中查找,所以@PropertySource中的application.properties不应该读取application test.properties吗?@Lance Java:是的,我使用的是new。将尝试从ApplicationContext中查找。@RobertMoskal我尝试过,但似乎也不起作用。您需要@ComponentScan或
来获取您的@Component我已进行了建议的更改,但它仍然没有从属性文件中加载值。。。你的类路径设置有什么我遗漏的吗?如何运行?是的,类路径已设置。上面D类中的代码提到了它是如何设置的。。我正在使用eclipse,我以->jUnit Test的方式运行,谢谢!在spring boot 2.1.2中,@TestPropertySource
在JUnit测试中与@ContextConfiguration
一起工作,而@PropertySource
不起作用。
@TestPropertySource("classpath:application-test.properties")
Properties configProps = new Properties();
InputStream iStream = new ClassPathResource("myapp-test.properties").getInputStream();
InputStream iStream = getConfigFile();
configProps.load(iStream);