您现在的位置是: 首页 > 后端开发 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 【后端开发】 4773人已围观 9446次浏览
简介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)
相关文章
- 记一次Java MessageFormat.format踩坑
- SpringBoot 2.x 文件上传出现 The field file exceeds its maximum permitted size of 1048576 bytes
- start.spring.io访问太慢,构建SpringBoot项目失败
- SqlServer 查询包含指定字段的存储过程
- SpringBoot @NotBlank 不生效问题
- springboot引入mybatis-plus后出现ClassNotFoundException: org.mybatis.logging.LoggerFactory
- Nestedset 出现 Node must exists. 错误解决方案
- 记一次 Mybatis-Plus 自动填充无效问题解决
- SpringBoot 2.x Security security.basic.enabled=false 失效问题解决
- SpringBoot 定时任务 多线程
点击排行
- 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 自动填充无效问题解决
- SpringBoot 2.x 文件上传出现 The field file exceeds its maximum permitted size of 1048576 bytes
- nuxt 项目完整部署方案
站长推荐
猜你喜欢
- Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR DISABLE You can't write or read against a disable instance
- nginx 出现 the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in 错误解决方案
- Vue 报错 Avoid using non-primitive value as key, use string/number value instead
- Linux no space left on device 出现设备上没有空间问题
- Nginx转发阿里云OSS内网流量
- Zookeeper 找不到或无法加载主类 org.apache.zookeeper.server.quorum.QuorumPeerMain
- 迁移 Docker 目录
- Mac 设置允许任何来源
- 自建Ngrok服务端
- CentOS 安装JDK