0

I have a junit + powermock test which is giving me NullPointerException after execution.

Error:

Running com.ebayenterprise.publicapi.events.dao.EventLogDaoTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.36 sec <<< FAILURE! - in com.ebayenterprise.publicapi.events.dao.EventLogDaoTest
testGetNextEventIdSequence(com.ebayenterprise.publicapi.events.dao.EventLogDaoTest)  Time elapsed: 0.05 sec  <<< ERROR!
java.lang.NullPointerException: null
    at com.ebayenterprise.publicapi.events.dao.EventLogDaoTest.testGetNextEventIdSequence(EventLogDaoTest.java:27)

EventLogDaoTest.java

public class EventLogDaoTest {

    private final DataSource dataSource = Mockito.mock(DataSource.class);
    private final PlatformTransactionManager txManager = Mockito.mock(PlatformTransactionManager.class);
    private static final long MOCK_EVENT_ID = 1001;
    private JdbcTemplate jdbcTemplate;
    private EventLogDao eventLogDao;

    @Before
    public void setup() {
        eventLogDao = new EventLogDao(dataSource, txManager);
    }

    @Test
    public void testGetNextEventIdSequence() {
        when(jdbcTemplate.queryForLong("select next-seq-value from table")).thenReturn(MOCK_EVENT_ID);
        long eventId = eventLogDao.getNextEventIdSequence();
        assertTrue(eventId == 1001);
    }

}

EventLogDao.java

@Repository
public class EventLogDao extends BaseEventLogDao {

    public EventLogDao(DataSource dataSource, PlatformTransactionManager transactionManager) {
        super(dataSource, transactionManager);
    }

}

BaseEventLogDao.java

public abstract class BaseEventLogDao {

    protected final JdbcTemplate jdbcTemplate;
    protected final PlatformTransactionManager transactionManager;

    public BaseEventLogDao(DataSource dataSource, PlatformTransactionManager transactionManager) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
        this.transactionManager = transactionManager;
    }

public long getNextEventIdSequence() {
        long eventIdSeq = jdbcTemplate.queryForLong(SELECT_EVENT_LOG_SEQUENCE_SQL);
        System.out.println("eventIdSeq = " + eventIdSeq);
        return eventIdSeq;
    }

}
Nital
  • 5,784
  • 26
  • 103
  • 195
  • 1
    How is `jdbcTemplate` instantiated in your test? – Morfic Jun 23 '17 at 19:46
  • Won't it get instantiated automatically in the constructor of `BaseEventLogDao`? – Nital Jun 23 '17 at 19:48
  • Even if it is, it won't exist in your test class. – ngreen Jun 23 '17 at 19:48
  • I'm talking about the one defined in your test, which you then try to use as a mock and define some behaviour for `when` it's called. – Morfic Jun 23 '17 at 19:50
  • So, do I have to manually `new` a JdbcTemplate ? I apologize in advance, I am not good a unit testing. – Nital Jun 23 '17 at 19:50
  • I understand the problem but not sure how to initialize it – Nital Jun 23 '17 at 19:58
  • Can anybody please guide on how can I initialize by JdbcTemplate in the test class ? – Nital Jun 26 '17 at 15:52
  • Instead of when() on `JdbcTemplate` (which is not related to the `JdbcTemplate` used by your code) you have to recommend on the `DataSource` based on how it's used by the JdbcTemplate... really `BaseEventLogDao` should take in a fully constructed `JdbcTemplate` so you *can* when on it. – Daniel Bickler Jun 27 '17 at 18:18

0 Answers0