IDEA 运行test的时候报错,读取properties中的port值失败

问题描述:

Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value '1-1'; nested exception is java.lang.NumberFormatException: For input string: "1-1"
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:46)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174)
  at org.springframework.core.env.AbstractPropertyResolver.convertValueIfNecessary(AbstractPropertyResolver.java:263)
  at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:91)
  at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:68)
  at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:546)
  at org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType.getPortProperty(ManagementPortType.java:57)
  at org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType.get(ManagementPortType.java:46)
  at org.springframework.boot.actuate.autoconfigure.web.server.OnManagementPortCondition.getMatchOutcome(OnManagementPortCondition.java:53)

现象:正常运行工程却是没报错,估计是@Test运行的时候哪些properties没有成功加载

分析:

  • 全局搜索“1-1”,没有搜索到相关字符串

  • 根据上述堆栈代码:

OnManagementPortCondition.getMatchOutcome(OnManagementPortCondition.java:53)

定位到是在依赖库:
compile('de.codecentric:spring-boot-admin-starter-client')中的
先将其注释掉,可以正常跑Test了。
说明问题与这个库相关;
根据上述堆栈进行Debug, 调试真的很重要,下断点调试:
定位如下代码片段:

static ManagementPortType get(Environment environment) {
        Integer serverPort = getPortProperty(environment, "server.");
        Integer managementPort = getPortProperty(environment, "management.server.");
private static Integer getPortProperty(Environment environment, String prefix) {
        return environment.getProperty(prefix + "port", Integer.class);
    }

调试发现这个port读取到的值居然是-1,而且读取properties中的字段“management.server.port”

打开application.yml配置文件进行查看

############################actuator配置#######################################
management:
  endpoints:
    web:
      exposure:
        include: "*" #“*”号代表启用所有的监控端点,可以单独启用,例如,health,info,metrics等
  server:
    servlet:
      context-path:
    ssl:
      enabled: false
    port: 1${server.port} # actuator访问的端口
  endpoint:

上述是yaml的配置写法,具体可以查看yaml语法,上述port的写法拼接起来就是:
"management.server.port" 对应前面源码中的字段key值。
而且配置文件中的配置写法是 1${server.port}

之前调试发现获取到的属性值是 -1 这就能解释报错“1-1”的问题了,源码中port是默认Integer类型的,但现在通过属性读取后是“1-1” 便出现类型转换错误。

正常跑程序是ok的,而跑@Test测试用例则不行,估计是跑@Test加载程序的时候,有一些配置没有初始化到,而下面的{server.port}用到的是前面配置的结果(仅仅猜测,刚刚学习,后续研究下单元测试的初始化流程)

总结:遇到问题先分析,根据堆栈进行调试,Debug the Code! ^_^

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,463评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,070评论 6 342
  • 原文链接:https://docs.spring.io/spring-boot/docs/1.4.x/refere...
    pseudo_niaonao阅读 10,187评论 0 9
  • 狼牙月悬空, 墨云祭苍穹。 通天揽宏癝, 紫极撼星骤。
    千玑珏阅读 1,118评论 0 2
  • 少年时,总念:情不知所起,一往而情深,生者可以死,死可以生。生而不可与死,死而不可复生,皆非情之至也。 过年时候看...
    木筱茜阅读 1,514评论 0 0