0

Im trying to test a method in a DAO which uses Calendar. This is the test I have so far:

@Test
public void testGetElectionInfo() throws SQLException {
    adminDao.getElectionInfo(1);
    verify(mockConn, times(1)).prepareStatement(anyString());
    verify(mockPreparedStmnt, times(1)).setInt(anyInt(), anyInt());
    verify(mockPreparedStmnt, times(1)).executeQuery();
    verify(mockResultSet, times(2)).next();
    verify(mockResultSet, times(1)).getString("name");
    verify(mockResultSet, times(1)).getTimestamp("startDate");
    verify(mockResultSet, times(1)).getTimestamp("endDate");
}

This is the DAO method:

public ElectionInfo getElectionInfo(int electionId) {
    ElectionInfo electionInfo = new ElectionInfo();
    try {
        con = sqlConnection.getConnection();

        stmt = con.prepareStatement(NamedQueries.GET_ELECTION_INFO);
        stmt.setInt(1, electionId);
        rs = stmt.executeQuery();

        if(rs.next()) {
            String name = rs.getString("name");
            Timestamp startDate = rs.getTimestamp("startDate");
            Timestamp endDate = rs.getTimestamp("endDate");

            Calendar startCalender = Calendar.getInstance();
            startCalender.setTime(startDate);

            Calendar endCalender = Calendar.getInstance();
            endCalender.setTime(endDate);

            electionInfo.setName(name);
            electionInfo.setStartDate(startCalender);
            electionInfo.setEndDate(endCalender);
        }

        return electionInfo;
    } catch (SQLException e) {
        LOGGER.log(Level.SEVERE, "Couldn't get the election info of the planned elections.", e);
    } finally{
        closeConnection();
    }
    return electionInfo;
}

When I run the test, it gives me a

java.lang.NullPointerException
    at java.util.Calendar.setTime

How can I resolve this issue? Could you please the post the code that solves this issue? That would help me alot!

Thanks in advance!

jbx
  • 21,365
  • 18
  • 90
  • 144
Khiem
  • 157
  • 6
  • 18
  • It is a static method, you need to use PowerMockito. – pvpkiran Jun 08 '18 at 10:31
  • Can you tell us which line number did this occur? Can you add two asserts at line 14 and 15 to verify that `startDate` and `endDate` are not `null`? – jbx Jun 08 '18 at 10:32
  • `verify(mockResultSet, times(1)).getTimestamp("startDate"); ` return something from this call, right now it returns a null causing it to throw a NPE – Antho Christen Jun 08 '18 at 10:32
  • Possible duplicate of [java: how to mock Calendar.getInstance()?](https://stackoverflow.com/questions/9275840/java-how-to-mock-calendar-getinstance) – Rafał Sokalski Jun 08 '18 at 10:32
  • @jbx it occurs in the Dao method at : startCalender.setTime(startDate); – Khiem Jun 08 '18 at 10:36
  • So `startDate` is probably `null`. Can you confirm it by adding `assert startDate != null` before that line? – jbx Jun 08 '18 at 10:37
  • When mocking the `ResultSet`, Mockito is returning `null` as your `startDate` and `endDate`. You need to explicitly tell it what to return. Refer to the answer by @anchreg – jbx Jun 08 '18 at 10:40

1 Answers1

3

The line, verify(mockResultSet, times(1)).getTimestamp("startDate"); mocks the call but doesn't ask the mocking library to return a Timestamp value, and so the library returns null causing it to throw a NPE. Try changing to,

verify(mockResultSet, times(1)).getTimestamp("startDate").thenReturn(new Timestamp(0)); and likewise for the other methods too.

Antho Christen
  • 1,369
  • 1
  • 10
  • 21
  • this did not solve the issue (andReturn didn't work), but when I saw this post, I came up with this: when(mockResultSet.getTimestamp("startDate")).thenReturn(new Timestamp(0)); when(mockResultSet.getTimestamp("endDate")).thenReturn(new Timestamp(0)); And this solved my issue! – Khiem Jun 08 '18 at 10:42
  • oops! corrected the method name in the answer. thanks for pointing it out! `andReturn` is in EasyMock :) – Antho Christen Jun 08 '18 at 10:44