Reputation: 167
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?
Upvotes: 1
Views: 1817
Reputation: 904
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.
Upvotes: 3