我需要从测试类中模拟Spring crudRepository findById(“ 111”)。get()
我的代码如下所示,即使我按照以下步骤操作,它也会返回null。
(1)我需要测试我的Service类,并创建了serviceTest类,并使用了mokito和powermock
(2)有一个方法,我需要让User给一个ID,其中服务类方法只是调用存储库findById(“ 111”)。get()并返回User对象
(3)修改这样的测试方法
//configuration codes working well
.
.
.
tesst123 () {
.
.
//here some code working well
.
.
.
when (mockRepository.findById("111").get()).thenReturn (new User());
}
Run Code Online (Sandbox Code Playgroud)
在上面,我可以嘲笑模拟存储库,并在调试时显示创建的对象。
但是,如果我在调试时评估了mockRepository.findById(“ 111”)部分,它将返回null
如果我在调试时评估了mockRepository.findById(“ 111”)。get()部分,它将返回nullPointer异常
****最后我播下.get()方法返回Optional.class,它是最终类
那么有什么方法可以模拟这种情况?