java如何模拟可选的自动连接字段
我试图用Mockito进行单元测试,但我找不到任何方法来模拟构造函数的可选自动连接字段
这是我的构造函数:
@Autowired
public BatchInputManager(
BatchInputContentRepository batchInputContentRepository,
Optional<List<BatchInputExecutor>> batchInputExecutors) {
// ...
}
下面是我如何模拟这些字段:
@InjectMocks
BatchInputManager batchInputManager;
@Mock
BatchInputContentRepository batchInputContentRepository;
@Mock
List<BatchInputExecutor> executors;
作为记录,BatchInputExecutor
类是一个抽象类,我在测试中定义了一个扩展它的类
当我运行代码时,应该包含BatchInputExecutor
的所有扩展类的可选项不是空的,而是空的;并且存储库不是空的
我应该如何模拟构造函数中可选字段的值
# 1 楼答案
我将在JUnit测试的
@BeforeEach/@BeforeAll
方法中设置mock# 2 楼答案
如果您真的想在这种情况下使用Mockito的注释:
改变这个
对此
并配置mockito以支持最终的类和方法。看到这个https://stackoverflow.com/a/40018295/10744129