【Exception】Cannot instantiate test(s): java.lang.SecurityException: Prohibited package name: java.cn

本文详细介绍了在单元测试过程中遇到的包名冲突问题,特别是当包名以java开头时,会触发java.lang.SecurityException异常。文章提供了具体的错误信息及分析,并给出了解决方案:将包名更改为cn.itplh.common.util。

案发现场

单元测试时

Cannot instantiate test(s): java.lang.SecurityException: Prohibited package name: java.cn.itplh.common.util

Process finished with exit code 1

原因分析

解决方案

在测试类中包名不能以java开头

将包改为cn.itplh.common.util就好了。

`Failed to instantiate [com.sf.boot.disconf.DisconfConfiguration] Constructor threw exception nested exception is java.lang.RuntimeException: java.lang.Exception: settings: disconf.conf_server_host cannot find` 这个错误表明在创建 `com.sf.boot.disconf.DisconfConfiguration` Bean 时,找不到 `disconf.conf_server_host` 配置项。以下是一些解决方案: ### 检查配置文件 确保在应用的配置文件(如 `application.properties` 或 `application.yml`)中正确配置了 `disconf.conf_server_host`。 #### `application.properties` 示例 ```properties disconf.conf_server_host=your_disconf_server_host ``` #### `application.yml` 示例 ```yaml disconf: conf_server_host: your_disconf_server_host ``` ### 检查配置加载顺序 确保配置文件被正确加载。可以通过在启动类中添加日志,确认配置文件是否被正确加载。 ### 检查环境配置 如果应用使用了多环境配置(如 `application-dev.properties`、`application-prod.properties` 等),确保当前环境下的配置文件中配置了 `disconf.conf_server_host`。 ### 检查依赖版本 确保使用的 Disconf 依赖版本是兼容的。有时候,依赖版本不兼容也会导致配置无法正确加载的问题。可以尝试更新或者回退 Disconf 相关依赖的版本。 ### 检查代码中是否有手动覆盖配置 检查代码中是否存在手动覆盖配置的情况。有时候,代码里的某些逻辑可能会覆盖配置文件中的配置,从而导致配置项丢失。 ### 示例代码检查 以下是一个简单的 Spring Boot 启动类示例,用于检查配置文件是否被正确加载: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.env.Environment; @SpringBootApplication public class YourApplication { public static void main(String[] args) { SpringApplication app = new SpringApplication(YourApplication.class); Environment env = app.run(args).getEnvironment(); System.out.println("disconf.conf_server_host: " + env.getProperty("disconf.conf_server_host")); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值