kanchi
kanchi

Reputation: 11

NullPointerException on mockito spring's NamedParameterJdbcTemplate

i have a DAO class that I am trying to write test cases

Here is modified reproducible code

the DAO class I am trying to test


@Component
public class SomeDAO {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    public boolean dataInTable(String id) {
        String sql = "SELECT COUNT(*) " +
                "FROM table1" +
                "WHERE id =:id";



        MapSqlParameterSource sqlParms = new MapSqlParameterSource();
        sqlParms.addValue("id", id

        try {
            int count = namedParameterJdbcTemplate.queryForObject(sql, sqlParms, Integer.class);
            return (count > 0) ? true : false;

        } catch (Exception e) {
            log.error("Error checking existence of id : " + id);
            throw e;
        }
    }

}

When i run the test, it throws an NullPointer on

int count = namedParameterJdbcTemplate.queryForObject(sql, sqlParms, Integer.class);

so i believe mockito is not returning a value on when, thenReturn

i searched around stack overflow and it looks like what i have should work, but I am getting NullPointerException.

I have also tried using the same value for sql string and a mapObject as the dao class instead of anyString() and anyMap()

but did not work either

Test case

@RunWith(MockitoJUnitRunner.class)
public class SomeDAOTest {

    @InjectMocks
    SomeDAO someDao

    @Mock
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);

    }

    @Test
    public void TestdataInTable(){

        //
        when(namedParameterJdbcTemplate.queryForObject(anyString(), anyMap(),eq(Integer.class))).thenReturn(1);
        assertEquals( true,someDao.dataInTable("123456"));
    }

}

Upvotes: 0

Views: 2044

Answers (2)

Vaibs007
Vaibs007

Reputation: 29

Adding this as it may help someone:

I had a similar issue but the reason was very different. I was getting null pointer exception not just on NamedParameterJdbcTemplate which was annotated with @Mock but also on @InjectMocks.

It so turn out that I was using Junit 5. The above solution will work with Junit 4. To make Mockito use Junit 5, an additional annotation is required. Also make sure you remove @RunWith(MockitoJUnitRunner.class) as this will cause failure. Instead use @ExtendWith(MockitoExtension.class.

Here is an example:

@ExtendWith(MockitoExtension.class) 
public class SomeDAOTest {

@InjectMocks
SomeDAO someDao

@Mock
NamedParameterJdbcTemplate namedParameterJdbcTemplate;

Upvotes: 1

S_learner
S_learner

Reputation: 334

Try this,

@Test
  public void TestdataInTable() {

    Mockito.when(namedParameterJdbcTemplate.queryForObject(Mockito.anyString(),
        Mockito.any(SqlParameterSource.class), Mockito.eq(Integer.class))).thenReturn(1);
    assertEquals(true, someDao.dataInTable("123456"));
  }
  1. The second parameter in queryForObject() is of type SqlParameterSource, which is not directly a map. So it should be, Mockito.any(SqlParameterSource.class).

The current version of your code displays warning in console, which is the best way to debug the issue.

[MockitoHint] TestDao.TestdataInTable (see javadoc for MockitoHint):
[MockitoHint] 1. Unused... -> at TestDao.TestdataInTable(TestDao.java:33)
[MockitoHint]  ...args ok? -> at SomeDao.dataInTable(SomeDao.java:26)

Upvotes: 3

Related Questions