How to pass 100% of coverage

Viewed 48
@Override
public int insertEvent(JobStatusDTO jobStatusDTO) throws AccountUpdateException {
        LOGGER.info("In insertAccountUpdateEvent- {}", jobStatusDTO);
        try {
            jdbcTemplate.execute("INSERT INTO EVENT_LOG (JOB_EXECUTION_ID, FILE_NAME, STATUS, TOTAL_EVENTS_RECD, TOTAL_EVENTS_PROCESSED, TOTAL_EVENTS_FAILED, FILE_PROCESS_START_TIME, FILE_PROCESS_END_TIME, PROCESS_MESSAGE_N) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",new PreparedStatementCallback<Boolean>() {
   @Override
    public Boolean doInPreparedStatement(PreparedStatement ps)throws SQLException, DataAccessException {
                            ps.setLong(1, jobStatusDTO.getJobId());
                            ps.setString(2, jobStatusDTO.getFileName());
                            ps.setString(3, jobStatusDTO.getJobStatus());
                            ps.setLong(4, jobStatusDTO.getTotalEvents());
                            ps.setLong(5, jobStatusDTO.getTotalSuccessEvents());
                            ps.setLong(6, jobStatusDTO.getTotalFailedEvents());
                            ps.setTimestamp(7, new Timestamp(jobStatusDTO.getJobStartTime().getTime()));
                            ps.setTimestamp(8, new Timestamp(jobStatusDTO.getJobEndTime().getTime()));
                            ps.setString(9, processErrorMessage(jobStatusDTO));
                            return ps.execute();
                        }

                    });
        } catch (DataAccessException e) {
            throw new Exception(Error.ERR_080.getErrorMessage(), e.getMessage());
        } catch (Exception e) {
            throw new Exception(Error.ERR_081.getErrorMessage(), e.getMessage());
        }
        return 0;
    }

I am using Spring framework for my project. When I Debug this it fails at doInpreparedStatement, as it is not getting inside of this method. I am not able to cover the PreparedStatementCallback, And due to this I am not able to achieve 100% coverage. I have written this test below, I am not sure if it is right or wrong as i am new to unit testing.


class AccountUpdateAuditDAOImplTest {

@InjectMocks
AccountUpdateAuditDAOImpl accountUpdateAuditDAOImpl;

@Mock
JdbcTemplate jdbcTemplate;

@Mock
ResultSet set;

@Mock
NamedParameterJdbcTemplate namedTemplate;

@SuppressWarnings("rawtypes")
@Captor
ArgumentCaptor argCaptor;

@BeforeEach
void setup() throws Exception {
    MockitoAnnotations.openMocks(this);
}

@SuppressWarnings("unchecked")
@Test
void testInsertAccountUpdateEvent() throws AccountUpdateException, Exception {
    when(jdbcTemplate.execute(Mockito.anyString(), Mockito.any(PreparedStatementCallback.class))).thenReturn(true);
    JobStatusDTO dTO = createMockDTO();
    PreparedStatement ps = mock(PreparedStatement.class);
    ps.setLong(1, dTO.getJobId());
    List<JobStatusDTO>   list = new ArrayList<JobStatusDTO>();
    dTO.setJobId(1);
    list.add(dTO);
    
    List<Map<String, Object>> value = new ArrayList<Map<String, Object>>();
    Map<String, Object> map =new HashMap<String, Object>();
    map.put("Job_Id", "1");
    map.put("File_Name", "RTAU_Btach");
    map.put("Job_Status", "Completed");
    map.put("Total_Event", "10");
    map.put("Success_Event", "7");
    map.put("Failure_Event", "3");
    value.add(map);
    
    Mockito.when(namedTemplate.queryForList(Mockito.anyString(), Mockito.anyMap())).thenReturn(value);
    assertDoesNotThrow(()->accountUpdateAuditDAOImpl.insertAccountUpdateEvent(dTO));
}


private JobStatusDTO createMockDTO() {
    JobStatusDTO jobStatusDTO = new JobStatusDTO();
    jobStatusDTO.setFileName("rtau_pulse_20220624104610853.txt");
    jobStatusDTO.setJobStatus("PARTIALLY COMPLETED");
    jobStatusDTO.setProcessMessage("Passed");
    jobStatusDTO.setTotalSuccessEvents(15l);
    return jobStatusDTO;
}

}

0 Answers
Related