Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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 Spring转换服务未正确初始化以进行测试_Java_Spring_Spring Boot_Spring Test_Spring Config - Fatal编程技术网

Java Spring转换服务未正确初始化以进行测试

Java Spring转换服务未正确初始化以进行测试,java,spring,spring-boot,spring-test,spring-config,Java,Spring,Spring Boot,Spring Test,Spring Config,我正在使用SpringBoot1.5.9编写一个小型rest服务器。我刚开始编写初始化代码,就被这种奇怪的行为所困扰 我有一个小测验- @RunWith(SpringRunner.class) @SpringBootTest public class TestMongo { @Autowired private MongoOperations mongoOps; @Test public void testMongoConnection() {

我正在使用SpringBoot1.5.9编写一个小型rest服务器。我刚开始编写初始化代码,就被这种奇怪的行为所困扰

我有一个小测验-

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestMongo {

    @Autowired
    private MongoOperations mongoOps; 

    @Test
    public void testMongoConnection() {
        assertFalse(mongoOps.collectionExists("test"));
    }
}
最初,application.properties被忽略。在我添加@SpringBootTest注释后,已读取application.properties,但开始出现以下错误

原因:org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 1错误字段“端口”上的对象“mongo”中的字段错误:值被拒绝 [${mongo.port:27017}];代码 [typeMismatch.mongo.port,typeMismatch.port,typeMismatch.int,typeMismatch]; 论据 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码[mongo.port,port];参数[];默认消息[port]; 默认消息[转换类型的属性值失败] 属性“port”的必需类型“int”为“java.lang.String”;嵌套 例外是 org.springframework.core.convert.ConverterNotFoundException:否 发现转换器能够从类型[java.lang.String]转换为 键入[int]]at org.springframework.boot.bind.PropertiesConfigurationFactory.checkForBindingErrors(PropertiesConfigurationFactory.java:359) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]位于 org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:276) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]位于 org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]位于 org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:330) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]。。。56通用框架 省略

我尝试过将这个端口声明为java.lang.Integer和int

配置bean如下所示-

@Configuration
@EnableConfigurationProperties(MongoProperties.class)
public class SpringConfiguration {

    private MongoProperties mongoPropertiesConfiguration;

    public MongoProperties getMongoConfiguration() {
        return mongoPropertiesConfiguration;
    }


    @Autowired
    public void setMongoConfiguration(MongoProperties mongoConfiguration) {
        this.mongoPropertiesConfiguration = mongoConfiguration;
    }



    public @Bean MongoClient mongoClient() {
        return new MongoClient(mongoPropertiesConfiguration.getHost(), mongoPropertiesConfiguration.getPort());
    }

    public @Bean MongoDbFactory mongoDbFactory() {
        return new SimpleMongoDbFactory(mongoClient(), mongoPropertiesConfiguration.getDb());
    }

    public @Bean MongoOperations mongoOperations() {
        return new MongoTemplate(mongoDbFactory());
    }
}

我确实使用@EnableAutoConfiguration注释而不是@SpringBootTest运行了测试。但这只适用于其中一项测试。我认为测试在哪个包中很重要,我认为@EnableAutoConfiguration可能不是正确的方法

我已经调试spring源代码一段时间了,没有任何线索

如果你有任何建议,请告诉我

编辑1: 根据请求,添加application.properties

mongo.host=localhost
mongo.port=${mongo.port:27017}
mongo.db=${mongo.db:synctool}

不确定你的语法

mongo.port=${mongo.port:27017}

spring尝试将${mongo.port:27017}转换为MongoProperties.port整数属性

无法将类型为“java.lang.String”的属性值转换为属性“port”所需的类型“int”

如果要为属性设置一些默认值
请参阅application.properties/.yaml?@spi添加了application.properties的内容。似乎没有转换器注册到转换服务。但我不知道为什么?不确定你的语法(以前从未见过)。。。尝试使用
mongo.port=27017
instead@spi语法还可以。我曾经在使用\@EnableAutoConfiguration时使用过该语法。我也尝试过你的建议,但在\@SpringBootTest中无效。这不是答案。如果你想猜,就用评论。
mongo.host=localhost
mongo.port=${mongo.port:27017}
mongo.db=${mongo.db:synctool}