似乎我在Spring 4.1.17中使用Spring Boot1.2.6.RELEASE做的任何事情都不起作用。我只想访问应用程序属性并在必要时使用test覆盖它们(无需使用hack手动注入Property tySource)
这不工作...
@TestPropertySource(properties = {"elastic.index=test_index"})
这也不是.
@TestPropertySource(locations = "/classpath:document.properties")
也不是这个...
@PropertySource("classpath:/document.properties")
完整的测试用例...
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
@TestPropertySource(properties = {"elastic.index=test_index"})
public class PropertyTests {
@Value("${elastic.index}")
String index;
@Configuration
@TestPropertySource(properties = {"elastic.index=test_index"})
static class ContextConfiguration {
}
@Test
public void wtf() {
assertEquals("test_index", index);
}
}
导致
org.junit.ComparisonFailure:
Expected :test_index
Actual :${elastic.index}
似乎3.x和4.x之间有很多相互矛盾的信息,我找不到任何可以肯定的东西。
如有任何见解,将不胜感激。干杯
您对@Value的使用需要一个Property tySourcesPlaceholderConfigrer bean来解析${...}
占位符。请在此处查看已接受的答案:@Value未通过Java配置的测试上下文设置
我使用了@TestPropertySource
的位置
,以覆盖(或添加)属性。
这对我有用(Spring4.2.4):
@TestPropertySource(locations = {
"classpath:test.properties",
"classpath:test-override.properties" })
但是下面这样的覆盖属性没有:
@TestPropertySource(
locations = {"classpath:test.properties"},
properties = { "key=value" })
尽管javadoc说这些属性具有最高优先级。也许是虫子?
更新
该错误应在Spring Boot版本1.4.0及更高版本中修复。请参阅关闭问题的提交。到目前为止,以呈现方式声明的属性应该优先。
事实证明,最好的方法(直到Spring修复这个疏忽)是使用PropertySourcesPlaceholderConfigurer
,它将引入test.properties(或任何你想要的东西),并@Import
或扩展该@Configuration
。
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import java.io.IOException;
@Configuration
public class PropertyTestConfiguration {
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws IOException {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(ArrayUtils.addAll(
new PathMatchingResourcePatternResolver().getResources("classpath*:application.properties"),
new PathMatchingResourcePatternResolver().getResources("classpath*:test.properties")
)
);
return ppc;
}
}
这允许您在application.properties中定义默认值,并在test.properties中覆盖它们。当然,如果您有多个方案,则可以根据需要配置< code > PropertyTestConfiguration 类。
并在单元测试中使用它。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class PropertyTests {
@Value("${elastic.index}")
String index;
@Configuration
@Import({PropertyTestConfiguration.class})
static class ContextConfiguration {
}
}
问题内容: 在Spring 4.1.17中使用Spring Boot 1.2.6.RELEASE似乎没有任何作用。我只想访问应用程序属性,并在必要时用测试覆盖它们(无需使用hack手动注入PropertySource) 这不起作用.. 也没有。 也不是 完整的测试用例.. 导致 似乎在3.x和4.x之间有很多相互矛盾的信息,我找不到可以肯定使用的任何信息。 问题答案: 事实证明,最好的方法(直到S
我是scala新手,junit测试用例不在集成测试中运行。 我在build.sbt中添加了 请帮助我运行集成测试
问题内容: 我有一些通用的设置代码,已将这些代码分解为标记为的方法。但是,不必为每个测试都运行所有这些代码。有没有一种标记方式,使得该方法仅在某些测试之前运行? 问题答案: 只需将不需要安装代码的测试移到单独的测试类中即可。如果您有一些测试通用的其他代码,可以帮助保留这些代码,请将其移到帮助程序类中。
问题内容: 我正在尝试为自定义方面编写Junit测试。这是Aspect类代码片段: 因此,只要关节点与切入点匹配,上述方面就会截获。它的工作正常。 但是我的问题是如何对该类进行单元测试。我有以下Junit测试: 因此,我在Junit中匹配切入点时被其拦截。但是,当调用该联合点时,我应该如何确定(可能是通过断言)我正在拦截? 我不能断言返回值,因为它除了执行联合点外没有其他特殊之处。因此,无论是按方
问题内容: 我在一个项目中工作,我们必须为我们所有的简单bean(POJO)创建单元测试。如果POJO由getter和setter组成,那么是否有必要为其创建单元测试?假设POJO大约100%的时间都可以正常工作吗? 问题答案: TDD中的规则是“测试所有可能破坏的东西” 吸气剂可以破坏吗?通常不会,因此我不必费心测试。此外,我的代码 做 测试肯定会调用吸气所以它 会 被测试。 我个人的规则是,我
我正在尝试为一个类编写测试,该类具有从属性文件注入的字段值。我试图在运行TestNG测试时利用注释获取其中的值,但它似乎忽略了我的属性文件。 有几十个类似的问题,我试着仔细阅读,并尽可能尝试它们的实现。但我的问题似乎略有不同,原因如下: > @TestProperty tSource和@Property tySource不适用于JUnit:谈论JUnit而不是TestNG(可能相关,也可能不相关?