问题描述
有人可以帮我处理下面的 Mock 对象吗?我想为 ServiceImpl 类编写一个模拟测试用例.我想模拟 OrderIF 接口:
Can someone please help me with the below Mock object. I want to write a mock test case for ServiceImpl class. I want to mock OrderIF interface:
服务的实现是:
我的代码给出 NullPoinerException:
My code give NullPoinerException:
推荐答案
@InjectMocks
不会实例化或模拟您的类.此注解用于将模拟注入此字段.
@InjectMocks
will not instantiate or mock your class. This annotation is used for injecting mocks into this field.
如果你想测试 serviceImpl
你需要这样模拟:
If you want to test serviceImpl
you will need to mock in this way:
要使其工作,您需要在 @Before
方法中使用 runner 或 MockitoAnnotations.initMocks(this);
.
To make it work you either need to use runner or MockitoAnnotations.initMocks(this);
in @Before
method.
这篇关于使用 Mockito 模拟接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!