java Mockito - 没有为其中一个测试用例注入模拟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28877754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mockito - Mock not being injected for one of the testcases
提问by Eva Mariam
I have a jsf spring application and using mockitofor my unit test. I keep getting NullPointerException
when i run my junit
test in iEmployeeService
mocking. There are not Exception
for iSecurityLoginService
.
我有一个 jsf spring 应用程序并使用mockito进行单元测试。NullPointerException
当我junit
在iEmployeeService
模拟中运行我的测试时,我不断收到。没有Exception
为iSecurityLoginService
。
Method to be mocked
被嘲笑的方法
@Autowired
IEmployeeService iEmployeeService;
@Autowired
ISecurityLoginService iSecurityLoginService;
public void addEvent() {
entityEventsCreate.setTitle(entityEventsCreate.getTitle());
entityEventsCreate.setModifiedBy(iSecurityLoginService
.findLoggedInUserId());
int eventId = iEmployeeService.addEmployeeTimeOff(entityEventsCreate);
}
My JUnit test is annotated with @RunWith(MockitoJUnitRunner.class)
我的 JUnit 测试用 @RunWith(MockitoJUnitRunner.class)
@Mock
ISecurityLoginService iSecurityLoginService;
@Mock
IEmployeeService iEmployeeService;
@InjectMocks
ServiceCalendarViewBean serviceCalendarViewBean = new ServiceCalendarViewBean();
@Before public void initMocks() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testSaveEvent() {
Mockito.when(iSecurityLoginService.findLoggedInUserId()).thenReturn(1);
serviceCalendarViewBean.getEntityEventsCreate().setTitle("Junit Event Testing");
Mockito.when(iSecurityLoginService.findLoggedInUserId()).thenReturn(1);
Mockito.when(iEmployeeService.addEmployeeTimeOff(Mockito.any(Events.class))).thenReturn(2);
serviceCalendarViewBean.addEvent();
}
回答by Brice
Not related to the question, but useful to know !
与问题无关,但知道有用!
If the test is annotated with @RunWith(MockitoJUnitRunner.class)
then MockitoAnnotations.initMocks(this);
is not necessary (it may even cause issues when injecting), the mockito runner performs injection and additional stuffs to validate mocks.
如果测试用@RunWith(MockitoJUnitRunner.class)
then 进行注释MockitoAnnotations.initMocks(this);
是不必要的(它甚至可能在注入时导致问题),mockito runner 执行注入和其他内容来验证模拟。
Also having both mock init mechanisms may cause trouble with injection and stubbing, this is due to the way the lifecyle of JUnit test and how mockito unit-integration code is used :
同时具有两种模拟初始化机制可能会导致注入和存根问题,这是由于 JUnit 测试的生命周期方式以及模拟单元集成代码的使用方式:
- The runner will create mocks and inject those mocks in the test object.
- Then the
@Before
methods kicks in and recreate new mocks, and may not perform injection as the object is already initialized.
- 运行程序将创建模拟并将这些模拟注入到测试对象中。
- 然后这些
@Before
方法启动并重新创建新的模拟,并且可能不会执行注入,因为对象已经初始化。
回答by Eva Mariam
I solved the problem..In my spring bean, i had 2 objects for the same service interface. so the mock was being set for the first interface object.
我解决了这个问题..在我的 spring bean 中,我有 2 个对象用于同一个服务接口。所以模拟是为第一个接口对象设置的。
Ex: in my bean i had,
例如:在我的豆子里,
@Autowired
IEmployeeService employeeService;
@Autowired
IEmployeeService iEmployeeService;
So the mock create for the IEmployeeservice interfaces was being inject for the first service object irrelevant for their names.
因此,IEmployeeservice 接口的模拟创建被注入第一个与其名称无关的服务对象。
@Mock
IEmployeeService iEmployeeService;
ie, the mock object 'iEmployeeService' was injected to beans 'employeeService' .
即,模拟对象 'iEmployeeService' 被注入到 bean 'employeeService' 中。
Thank you for all those who helped..:)
感谢所有帮助过的人..:)
回答by Evgeniy Dorofeev
Try to add this
尝试添加这个
@Before
public void initMocks() {
MockitoAnnotations.initMocks(this);
}
回答by Praveen
I had a similar problem and after few researches, I found that it looks like the @InjectMocks
were not working and failing silently without injecting the @AutoWired
private objects,
我有一个类似的问题,经过一些研究,我发现它看起来好像@InjectMocks
没有注入@AutoWired
私有对象就无法正常工作和失败,
Solution :Change the design by making the dependencies visible through constructor,
解决方案:通过构造函数使依赖项可见来更改设计,
IEmployeeService iEmployeeService;
ISecurityLoginService iSecurityLoginService;
@Autowired
public ServiceCalendarViewBean(final IEmployeeService iEmployeeService,
final ISecurityLoginService iSecurityLoginService){
this.iEmployeeService=iEmployeeService;
this.iSecurityLoginService=iSecurityLoginService;
}
This linkhelped me in identifying how to work on @Autowired
objects which is not visible
此链接帮助我确定如何处理@Autowired
不可见的对象