您现在的位置是: 首页 > 后端开发 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 【后端开发】 3771人已围观 7448次浏览
简介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)
相关文章
- 记一次 Mybatis-Plus 自动填充无效问题解决
- Laravel项目出现could not be opened: failed to open stream: Permission denied
- SqlServer 查询包含指定字段的存储过程
- springboot引入mybatis-plus后出现ClassNotFoundException: org.mybatis.logging.LoggerFactory
- Nestedset 出现 Node must exists. 错误解决方案
- Laravel Dingo/api 出现 The version given was unknown or has no registered routes.报错
- 类 BASE64Decoder 程序包 sun.misc 找不到符号
- 获取阿里云CDN真实IP
- Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR DISABLE You can't write or read against a disable instance
- SqlServer 刷新所有视图
点击排行
- 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
站长推荐
猜你喜欢
- SpringBoot @NotBlank 不生效问题
- Mac 设置允许任何来源
- 常见端口号
- Android SDK版本号与API级别的对照表
- Vue 报错 Avoid using non-primitive value as key, use string/number value instead
- Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR DISABLE You can't write or read against a disable instance
- Oracle ORA-12541:TNS:no listener错误解决方法
- SqlServer字符串处理
- 记一次 Mybatis-Plus 自动填充无效问题解决
- 在更新python时出现dyld: Library not loaded: /usr/local/opt/libpsl/lib/libpsl.5.dylib,Reason: image not found错误解决方案