I am new to Mockito and trying to mock a service method. However, Mockito is not able to mock the function correctly.
Here is part of the test code:
@Mock
ConditionalLimitDao conditionalLimitDao;
@InjectMocks
ConditionalLimitFilingServiceImpl conditionalLimitFilingService;
Mockito.when(conditionalLimitDao.getAllConditionalLimitProductGroups()).thenReturn(Arrays.asList(clpg1));
The class:
public class ConditionalLimitFilingServiceImpl implements ConditionalLimitFilingService {
@Inject
private ConditionalLimitDao conditionalLimitDao;
@Override
public List<ConditionalLimitFiling> getConditionalLimitFiling(String filingMonth, Date reportDate)
throws SQLException, RemoteException {
conditionalLimitProductGroups = getConditionalLimitProductGroups();
return something;
}
private List<ConditionalLimitProductGroup> getConditionalLimitProductGroups() {
return conditionalLimitDao.getAllConditionalLimitProductGroups();
}
}
I am expecting getAllConditionalLimitProductGroups()
to return a non-empty list. However, it's returning an empty list. Can anyone help?
I think you are forgetting to add this line
MockitoAnnotations.initMocks(this);
This line of code should be placed in a @Before
method to initialize mocks for every test.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.