Java 在测试中读取xml资源之前的spring属性源
在SpringBoot1.4中,在主类中有一个导入xml资源的配置类Java 在测试中读取xml资源之前的spring属性源,java,spring,testing,spring-boot,functional-testing,Java,Spring,Testing,Spring Boot,Functional Testing,在SpringBoot1.4中,在主类中有一个导入xml资源的配置类 @ImportResource("classpath:app-config-c3p0.xml") 在src/test/resources/application.properties中,我提供了 datasource.database.master.pool-size=1 但我仍然面临这个问题,我尝试添加订单和@PropertySource以及@TestPropertySource,但都没有成功 以下是例外 Caused
@ImportResource("classpath:app-config-c3p0.xml")
在src/test/resources/application.properties中,我提供了
datasource.database.master.pool-size=1
但我仍然面临这个问题,我尝试添加订单和@PropertySource
以及@TestPropertySource
,但都没有成功
以下是例外
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [int] for property 'maxPoolSize'; nested exception is java.lang.NumberFormatException: For input string: "${datasource.database.master.pool-size}"
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:596)
at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:603)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:216)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1532)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1491)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1231)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
... 54 common frames omitted
Caused by: java.lang.NumberFormatException: For input string: "${datasource.database.master.pool-size}"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.valueOf(Integer.java:766)
at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:208)
at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:113)
at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:468)
at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:441)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:199)
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576)
... 60 common frames omitted
它在不运行测试时工作正常,因为属性是从SpringCloudConfigServer读取的
下面是测试用例
@WebMvcTest(VCController.class)
@RunWith(SpringRunner.class)
@TestPropertySource("classpath:test.properties")
public class VCControllerTest {
@MockBean
private VCGateway vCGateway;
@Autowired
private MockMvc mvc;
@Test
public void testCreateVoucher() throws Exception {
int timeout = 10;
CreateVC createVC = new CreateVC(timeout);
CreatedVCModel createdVCModel = new CreatedVCModel();
given(vCGateway.create(createVC)).willReturn(createdVCModel);
mvc.perform(post("/v1/vc")
.content(json(createVC))
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated());
}
}
此错误通常在未解析属性时发生
为测试属性在src/test/resources
下添加一个resources
目录,这应该会起作用。是否尝试将propertySourcesplaceConfigurer
bean添加到java配置中,以解析@Value注释中的${…}表达式
通过添加此内部类:
@Configuration
public static class TestContext{
@Bean
public static PropertySourcesPlaceholderConfigurer properties(){
return new PropertySourcesPlaceholderConfigurer();
}
}
如果需要将@ContextConfiguration(loader=AnnotationConfigContextLoader.class)添加到VCControllerTest
测试类以加载@Configuration类。在我这边,application.properties的路径是src/test/resources
。但它仍然不起作用:(.app-config-c3p0.xml在哪里?它在src/main/resources
中。我只想覆盖env属性。添加您的测试用例。@M.Deinum testcase added。感谢您的帮助!您的解决方案成功了,但我不得不做一些更改,而不是PropertyPlaceHolderConfiger
我必须使用PropertySourcePlaceHolderconfiguer
因为一些属性没有加载,我在文档中发现它更喜欢使用属性资源占位符配置器
。其次,它没有添加@ContextConfiguration
,为什么您认为我应该添加它?提到的@ContextConfiguration是一个开销,因为它被spring boot自动配置,因此..您可以放弃它。谢谢您注意到错误。PropertyPlaceHolderConfigure
不支持@Value注释。我用正确的bean定义更新了我的答案。