您现在的位置是: 首页 > 后端开发 SpringBoot 启动测试时出现提示 Test class should have exactly one public zero-argument constructor
SpringBoot 启动测试时出现提示 Test class should have exactly one public zero-argument constructor
2020-09-11 【后端开发】 3791人已围观 7480次浏览
简介SpringBoot 启动测试时出现提示 Test class should have exactly one public zero-argument constructor
SpringBoot 项目中用到了 Lombok 插件来简化代码的编写量,实际项目使用过程中,Controller 或 Service 中,要引用其他接口,之前一般都是采用的 @Autowired 注解,但是在部分文件中,使用该注解时,下方会出现黄色的波浪线提示,有些解决方案都是说关闭该提示即可。实际上还有另外一种解决方法,那就是使用 Lombok 插件的 @AllArgsConstructor 注解,来实现所有需要使用 @Autowired 注解来引用的方法的全参构造函数,比如
@Slf4j
@AllArgsConstructor
@RestController("webIndexController")
@RequestMapping("/api/web/index")
public class IndexController {
......
private final IIndexService indexService;
......
}
这样就不需要每个 Service 方法上都写上 @Autowired 注解了。
但是,同样的方法并不适用于测试用例中,比如下面的代码在测试用例启动时就会提示 Test class should have exactly one public zero-argument constructor 错误
@Slf4j
@AllArgsConstructor
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductServiceTest {
......
private final IProductService productService;
......
}
使用这种方式引用进来的 Service 方法,是无法使用的,会提示 Test class should have exactly one public zero-argument constructor 错误,这里根据提示,加上无参构造函数 @NoArgsConstructor ,就会提示另一个错误 java.lang.IllegalArgumentException: Test class can only have one constructor ,所以,在测试用例中,最好的方式还是 @Autowired 注解形式,也就是下面的代码
@Slf4j
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductServiceTest {
......
@Autowired
private IProductService productService;
......
}
以这样的形式来引用 Service 方法,在实际使用的过程中就不会出现上面的两种错误
很赞哦! (0)
相关文章
- PHP 中list()出现Undefined offset: 0错误
- Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR DISABLE You can't write or read against a disable instance
- SqlServer 查询包含指定字段的存储过程
- 获取阿里云CDN真实IP
- SpringBoot 2.x Security security.basic.enabled=false 失效问题解决
- SpringBoot @NotBlank 不生效问题
- Nestedset 出现 Node must exists. 错误解决方案
- Laravel项目出现could not be opened: failed to open stream: Permission denied
- mac idea spring boot 启动慢
- 类 BASE64Decoder 程序包 sun.misc 找不到符号
点击排行
- Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR DISABLE You can't write or read against a disable instance
- Debian apt 使用国内镜像
- RocketMQ 出现 sendDefaultImpl call timeout 问题
- 类 BASE64Decoder 程序包 sun.misc 找不到符号
- SpringBoot @NotBlank 不生效问题
- 记一次 Mybatis-Plus 自动填充无效问题解决
- nuxt 项目完整部署方案
- SpringBoot 2.x 文件上传出现 The field file exceeds its maximum permitted size of 1048576 bytes
站长推荐
猜你喜欢
- 解决Mac突然没声音的问题
- SqlServer 查询包含指定字段的存储过程
- 【代码片段】MySQL 查看数据库所有表注释
- Linux 查看磁盘、磁盘分区、挂载磁盘、卸载磁盘操作
- Vue 路由跳转错误 NavigationDuplicated: Avoided redundant navigation to current location
- Nginx转发阿里云OSS内网流量
- SpringBoot 集成 Elasticsearch 7.8.1 出现错误 Factory method 'elasticsearchRestHighLevelClient' threw exception; nested exception is java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS
- Python自动生成和安装requirements.txt文件中的依赖
- SpringBoot 启动提示 Requested bean is currently in creation: Is there an unresolvable circular reference?
- Redis 提示 MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk 解决方案