【问题标题】:Spring conversion service not getting initialized correctly for testSpring 转换服务未正确初始化以进行测试
【发布时间】:2018-06-09 08:05:09
【问题描述】:

我正在使用spring boot 1.5.9 写一个小的rest server。我刚开始使用初始化代码,却被这种奇怪的行为困住了。

我有一个小测试-

@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];论据 [];默认消息[端口]]; 默认消息[无法转换类型的属性值 'java.lang.String' 到属性'port' 所需的类型'int';嵌套的 例外是 org.springframework.core.convert.ConverterNotFoundException:否 发现转换器能够从类型 [java.lang.String] 转换为 类型 [int]] 在 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());
    }
}

还有

@ConfigurationProperties(prefix="mongo")
public class MongoProperties {

    private String host;
    private Integer port;
    private String db;

    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    public Integer getPort() {
        return port;
    }
    public void setPort(Integer port) {
        this.port = port;
    }
    public String getDb() {
        return db;
    }
    public void setDb(String db) {
        this.db = db;
    }

}

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

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

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

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

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

【问题讨论】:

  • application.properties/.yaml 中有什么?
  • @spi 添加了 application.properties 的内容。
  • 看起来没有转换器注册到转换服务。但我不知道为什么?
  • 不确定您的语法(以前从未见过)...尝试改用mongo.port=27017
  • @spi 语法没问题。使用 \@EnableAutoConfiguration 时,我曾经使用过该语法。我也用 \@SpringBootTest 尝试了你的建议。

标签: java spring spring-boot spring-test spring-config


【解决方案1】:

不确定你的语法

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

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

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

如果你想为你的属性设置一些默认值 见Spring-boot: set default value to configurable properties

【讨论】:

  • 这不是答案。如果您想猜测,请使用 cmets。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 1970-01-01
  • 2015-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多