I am getting null pointer exception at getSession(). Unable to write JUnit for it to mock the method. Kindly help on the same.
public void updateData (List<Integer> cashFlows) {
LOGGER.debug("Inside updateData method in ");
LOGGER.debug("cashFlows Size " +cashFlows.size());
try {
String initialQuery = Constants.UPDATE_QUERY +cashFlows;
initialQuery = initialQuery.replace("[", "(");
initialQuery = initialQuery.replace("]", ")");
LOGGER.debug("initialQuery: " + initialQuery);
Query query = getSession().createNativeQuery(initialQuery);
int updatedRows = query.executeUpdate();
LOGGER.debug("Updated Rows : " + updatedRows);
}
catch(Exception e) {
LOGGER.error("Error in updateData : " + e);
}
}
How could I write it's Junit as it is giving exception at getSession().createNativeQuery(initialQuery)
JUnit Code
@BeforeEach
public void setUp() throws SQLException {
when(getSession()).thenReturn(hibernateSession);
}
@Test
public void updateDataTest() {
List<Integer> cashIdList=new ArrayList<Integer>();
cashIdList.add(10);
daoImpl.updateData(cashIdList);
verify(tmsCfsCashflowsDaoImpl,times(1)).updateData(cashIdList);
}