Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧启动测试用例不';不要使用自定义转换服务_Java_Spring_Spring Boot_Dependency Management - Fatal编程技术网

Java 弹簧启动测试用例不';不要使用自定义转换服务

Java 弹簧启动测试用例不';不要使用自定义转换服务,java,spring,spring-boot,dependency-management,Java,Spring,Spring Boot,Dependency Management,我正在尝试编写一个带有Spring启动测试的集成测试用例 我定制了ConversionService,以了解新的java.time类型: @Configuration public class ConversionServiceConfiguration { @Bean public static ConversionService conversionService() { final FormattingConversionService reg = new

我正在尝试编写一个带有Spring启动测试的集成测试用例

我定制了
ConversionService
,以了解新的
java.time
类型:

@Configuration
public class ConversionServiceConfiguration {
    @Bean
    public static ConversionService conversionService() {
        final FormattingConversionService reg = new DefaultFormattingConversionService();
        new DateTimeFormatterRegistrar().registerFormatters(reg);
        return reg;
    }
}
然后期待它能起作用:

@Component
class MyServiceConfig {
    @Value("${max-watch-time:PT20s}")
    private Duration maxWatchTime = Duration.ofSeconds(20);
}
在正常的
SpringApplication.run下运行时,这似乎工作正常。但是,在我的测试用例中:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT, classes= {
    MyServiceMain.class,
    AttachClientRule.class
})
public class MyTest {
    @Inject
    @Rule
    public AttachClientRule client;

    @Test(expected=IllegalArgumentException.class)
    public void testBad() throws Exception {
        client.doSomethingIllegal();
    }
}
它爆炸了:

原因:org.springframework.beans.factory.unsatifiedDependencyException:创建名为“AttachClientRule”的bean时出错:通过构造函数参数0表示的未满足依赖项:

创建名为“MyServiceConfig”的bean时出错:通过字段“maxWatchTime”表示的未满足的依赖关系:未能将类型为[java.lang.String]的值转换为所需类型[java.time.Duration]

嵌套异常为java.lang.IllegalStateException:无法将类型为[java.lang.String]的值转换为所需类型[java.time.Duration]:未找到匹配的编辑器或转换策略

深入查看执行实际转换的
TypeConverterDelegate
,它似乎捕获了从
DefaultListableBeanFactory
上的字段使用的
ConversionService
。在设置该字段的位置设置观察点,我发现
AbstractApplicationContext.refresh()
方法:

// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();  // <--- MyServiceConfig initialized here
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory); // <--- DefaultListableBeanFactory.conversionService set here!!!
// Last step: publish corresponding event.
finishRefresh();
这会迫使初始化提前进行,但感觉不是正确的解决方案(至少没有这样的文档记录)

我哪里出错了? 弹簧4.3.0,弹簧防尘套1.4.0M3

编辑

现在我发现了另一种失败的方法!不使同一配置类实现环境感知

@Override
public void setEnvironment(Environment environment) {
    ((AbstractEnvironment) environment).setConversionService(conversionService());
}
我发现
PropertySourcesPropertyResolver
使用了错误的(默认)
ConversionService
。这让我快发疯了

原因:java.lang.IllegalArgumentException:无法将值[PT15s]从源类型[String]转换为目标类型[Duration] 位于org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:94) 位于org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:65) 位于org.springframework.core.env.AbstractPropertyResolver.getProperty(AbstractPropertyResolver.java:143) 位于org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:546) 在com.mycorp.DoSomething.go(DoSomething.java:103)


尝试从
conversionService
bean定义中删除
static
关键字。

Spring Boot开发人员已经确认,这是一个文档记录不完整的问题,不能按规定工作:

谢谢,但这似乎没有帮助。
@Override
public void setEnvironment(Environment environment) {
    ((AbstractEnvironment) environment).setConversionService(conversionService());
}