Spring Boot测试出现‘systemProperties已废弃’警告的原因与解决
Spring Boot测试中
systemProperties弃用警告问题(未显式使用仍触发) 问题概述
使用@SpringBootTest注解进行Spring Boot测试时,收到以下警告:
Parameter 'systemProperties' is deprecated: Use systemPropertyVariables instead.
但并未在代码中显式使用systemProperties参数,需明确该警告的触发原因及解决办法。
显式使用场景的替换方案
如果代码中确实显式使用了systemProperties,可直接替换为systemPropertyVariables:
原弃用写法
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {"key1=value1", "key2=value2"}, systemProperties = {"sysKey1=sysValue1", "sysKey2=sysValue2"})
替换后写法
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {"key1=value1", "key2=value2"}, systemPropertyVariables = {"sysKey1=sysValue1", "sysKey2=sysValue2"})
未显式使用却触发警告的原因
- 依赖内部引用:当前使用的Spring Shell等组件,或Spring Boot测试自动配置类,可能在内部封装的测试逻辑中使用了弃用的
systemProperties参数,导致警告传递到业务测试代码。 - 注解默认属性传递:
@SpringBootTest的父注解或关联配置类,可能默认携带了systemProperties的引用,即使业务代码未显式声明,也会触发静态代码分析警告。 - IDE静态分析误报:部分IDE的代码检查工具可能误判注解的属性引用,可通过清理缓存验证。
解决办法
- 显式声明空的
systemPropertyVariables:在@SpringBootTest中添加systemPropertyVariables = {},覆盖默认的弃用参数引用,消除警告:@SpringBootTest(systemPropertyVariables = {}) - 检查自定义测试配置:排查项目中是否存在继承或导入的自定义测试配置类,确认是否间接使用了
systemProperties参数,如有则替换为systemPropertyVariables。 - 升级依赖版本:等待或升级到修复该问题的Spring Boot/Spring Shell后续小版本,从根源解决内部引用问题。
- 清理IDE缓存:若为IDE误报,执行清理缓存并重启IDE操作,重新检查警告是否消失。
当前使用的Spring相关依赖版本
mvn dependency:tree | grep spring [INFO] +- org.springframework.shell:spring-shell-starter:jar:3.1.5:compile [INFO] | +- org.springframework.shell:spring-shell-autoconfigure:jar:3.1.5:compile [INFO] | | \- org.springframework.boot:spring-boot-autoconfigure:jar:3.1.5:compile [INFO] | +- org.springframework.shell:spring-shell-core:jar:3.1.5:compile [INFO] | | +- org.springframework.boot:spring-boot-starter-validation:jar:3.1.5:compile [INFO] | | +- org.springframework:spring-messaging:jar:6.0.13:compile [INFO] | | | \- org.springframework:spring-beans:jar:6.0.13:compile [INFO] | +- org.springframework.shell:spring-shell-standard:jar:3.1.5:compile [INFO] | +- org.springframework.shell:spring-shell-standard-commands:jar:3.1.5:compile [INFO] | +- org.springframework.shell:spring-shell-table:jar:3.1.5:compile [INFO] | | \- org.springframework:spring-context:jar:6.0.13:compile [INFO] | | +- org.springframework:spring-aop:jar:6.0.13:compile [INFO] | | \- org.springframework:spring-expression:jar:6.0.13:compile [INFO] | \- org.springframework.boot:spring-boot-starter:jar:3.1.5:compile [INFO] | +- org.springframework.boot:spring-boot:jar:3.1.5:compile [INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:3.1.5:compile [INFO] \- org.springframework.boot:spring-boot-starter-test:jar:3.1.5:test [INFO] +- org.springframework.boot:spring-boot-test:jar:3.1.5:test [INFO] +- org.springframework.boot:spring-boot-test-autoconfigure:jar:3.1.5:test [INFO] +- org.springframework:spring-core:jar:6.0.13:compile [INFO] | \- org.springframework:spring-jcl:jar:6.0.13:compile [INFO] +- org.springframework:spring-test:jar:6.0.13:test
内容的提问来源于stack exchange,提问作者MaduKan
相关产品推荐
相关产品推荐

