I am testing my test class using Mockito (v4.7.0), I have mocked all the layers starting from Service to DAO layer which is where getJdbcTemplate is used to retrieve records from DB and loop through it to apply a business logic. When I run the test,
1> The mocked returned list within the DAO method is 0 for some reason.
2> I also found that the control hasn't traversed the rows to apply the business logic.
Can anyone please guide me how to proceed with this. Below are the sample test class and also the sample rowmapper block. I have listed what I have already tried that did not work, out in below code. Much appreciated if anyone can point me in the right direction.
@RunWith(MockitoJUnitRunner.class)
public class AToolControllerMockitoTest {
@Rule
public MockitoRule initRule = MockitoJUnit.rule();
@InjectMocks
private AToolController aToolController;
@Mock
MandatoryAssociationsDTO mandatoryAssociationsDTO;
@Before
public void init() throws NamingException, AppException {
MockitoAnnotations.openMocks(this);
}
@Test
public final void testDoMandatoryAssocaitionsist() throws AppException {
ExtDTO extDTO;
Mockito.when(mandatoryAssociationsDTO.getPlan_id()).thenReturn(1234);
Mockito.when(mandatoryAssociationsDTO.getSortOrder()).thenReturn("ASC");
List<MandatoryAssociationsDTO> expectedresultList = new ArrayList<>();
expectedresultList.add(new MandatoryAssociationsDTO());
expectedresultList.add(new MandatoryAssociationsDTO());
expectedresultList.add(new MandatoryAssociationsDTO());
expectedresultList.add(new MandatoryAssociationsDTO());
JdbcTemplate mockTemplate = Mockito.mock( JdbcTemplate.class);
//Example 1 did not work
// Mockito.when(mockTemplate.query(this.getQuery().toString(), ArgumentMatchers.<Object[]>any(), ArgumentMatchers.<RowMapper<MandatoryAssociationsDTO>>any())).thenReturn(expectedresultList);
//Example 2 did not work
// Mockito.when(mockTemplate.query(this.getQuery().toString(), ArgumentMatchers.<Object[]>any(),
// (ResultSetExtractor<T>) ArgumentMatchers.any(MandatoryAssociationsDTO.class)));
//Example 3 did not work
// Mockito.when(mockTemplate.query(
// Mockito.anyString(),
// Mockito.any(Object[].class),
// ArgumentMatchers.<RowMapper<MandatoryAssociationsDTO>>any())
// ).thenReturn(expectedresultList);
//Example 4 did not work
// Mockito.when(mockTemplate.query( Mockito.anyString(), ArgumentMatchers.<Object[]>any(),
// ArgumentMatchers.<RowMapper<MandatoryAssociationsDTO>>any())).thenReturn(expectedresultList);
//Example 5 did not work
Mockito.when(mockTemplate.query(Mockito.anyString(), ArgumentMatchers.<Object[]>any(), Mockito.any(ResultSetExtractor.class))).thenReturn(expectedresultList);
AToolService ptService = Mockito.spy(new AToolService());
AToolDAOImpl ptDAO = Mockito.spy(AToolDAOImpl.class);
ptDAO.setJdbcTemplate(mockTemplate);
ptService.setPtDAO(ptDAO);
aToolController.setPtService(ptService);
extDTO = aToolController.getMandatoryAssociationsList(mandatoryAssociationsDTO);
assertEquals(4, ((List<MandatoryAssociationsDTO>) extDTO.getData()).size());
}
}
And the rowmapper block looks as below which is not being traversed or the return list populated with 4 mocked objects.
matches = getJdbcTemplate().query(sqlStr, new ParameterizedRowMapper<MandatoryAssociationsDTO>() {
public MandatoryAssociationsDTO mapRow(ResultSet rs, int rowNum) throws SQLException {
try {
MandatoryAssociationsDTO bean = new MandatoryAssociationsDTO();
//Some business logic here for resultset get operations.
//I would like to traverse this block via mock if possible.
return bean;
} catch (SQLException e) {
logger.error("SQLException: getMandatoryAssociationsList()", e);
throw e;
}
}
});
System.out.println("Matches count: " matches.size()); //is 0
CodePudding user response:
You are moking the wrong query
method. The one you are moking is:
query(String query, Object[] param, RowMapper<T> rowMapper)
but in your code you are calling
query(String query, ParameterizedRowMapper<T> rowMapper)
Mocking the correct method will fix the issue:
Mockito.when(mockTemplate.query(Mockito.anyString(), Mockito.any(ParameterizedRowMapper.class))).thenReturn(expectedresultList);