NullPointerException on mockito spring's NamedParameterJdbcTemplate - spring-boot

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"));
}
}

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"));
}
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)

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;

Related

Dependency injection with mockito example

I am very new with Mockito and I don't get the following example (classes were provided, only test to write) and how to solve it.
What I try to do is use a test double for the supplier so that we can control the returned greeting in the test and assert that the GreetingService does not modify the greeting message in any way. Then assert that the returned greeting string is equal to "Hello Andy.".
public class Greeting {
private final String template;
public Greeting(String template) {
this.template = template;
}
public String forName(String world) {
return String.format(template, world);
}
}
#Component
public class GreetingService {
private final Supplier<Greeting> greetingSupplier;
public GreetingService(Supplier<Greeting> greetingSupplier) {
this.greetingSupplier = greetingSupplier;
}
public String greet(String name) {
return greetingSupplier.get().forName(name);
}
}
#Component
public class RandomGreetingSupplier implements Supplier<Greeting> {
private final List<Greeting> greetings = Arrays.asList(
new Greeting("Hello %s."),
new Greeting("Hi %s!"),
);
private final Random random = new Random();
#Override
public Greeting get() {
return greetings.get(random.nextInt(greetings.size()));
}
}
#SpringBootTest
public class GreetingServiceTest {
#Autowired
GreetingService greetingService;
#MockBean
Supplier<Greeting> greetingSupplier;
#Test
void getGreetingForPerson() {
String name = "Andy";
// that test cannot know which greeting will be returned by the supplier
// WHY IS IT NULLPOINTEREXCEPTION AFTER INITIALIZING #MockBean
//String greeting = greetingService.greet(name);
//assertThat(greeting).contains(name);
// WROTE SUCH TEST HERE -> NullPointerException WHY?
Mockito.when(greetingSupplier.get().forName(name)).thenReturn("Hello %s.");
assertThat(greetingSupplier.equals("Hello Andy."));
// THIS IS WORKING & TEST PASSED BUT I GUESS ITS WRONG?
Mockito.when(greetingSupplier.get()).thenReturn(new Greeting("Hello %s."));
assertThat(greetingSupplier.equals("Hello Andy."));
}
}
Mockito.when(greetingSupplier.get().forName(name)).thenReturn("Hello %s.");
You can't chain calls like that, you need to produce intermediate results, like
Supplier<Greeting> supplier = mock(Supplier.class);
Mockito.when(supplier).forName().thenReturn("Hello %s.");
Mockito.when(greetingSupplier.get()).thenReturn(supplier);
For dependency injection, you need to create the subject under test with the mocked Supplier. You can do that in a #Before method for example.
Your mocking is wrong.
Mockito.when(greetingSupplier.get().forName(name)).thenReturn("Hello %s.");
You mocked Supplier<Greeting> and the default behavior is to return null. So when you call greetingSupplier.get() in your first line it returns null. You directly chain forName which nou basicall is null.forName which leads to an error.
Your second part is actually (kind of) correct.
Mockito.when(greetingSupplier.get()).thenReturn(new Greeting("Hello %s."));
You now properly return a response from greetingSupplier.get(). Instead of chaining the call.
However I would argue that your excercise is wrong. Why? When using a Supplier<?> in Spring it actually is a lazy beanFactory.getBean call. You can lazily inject dependencies this way. You should have a mock for Greeting which returns a hardcoded String which you can check.

Spring boot with Mockito mocking NamedParameterJdbcTemplate

Trying to unit test a method which is defined as :
public void myMethod(List<? extends MyModel> model){
int[] result = namedParameterJdbcTemplate.batchUpdate("update query", SqlParameterSourceUtils.createBatch(model));
}
In my test class i am defining test method as
class MyTestClass{
#Mock
NamedParameterJdbcTemplate namedParameterJdbcTemplate;
#InjectMocks
MyDao dao;
#Test
public void testMyMethod() {
final int[] rowsAffected = new int[]{1,2};
when(namedParameterJdbcTemplate.batchUpdate(any(), SqlParameterSourceUtils.createBatch(Arrays.asList(anySet())))).thenReturn(rowsAffected);
List<MyModel> myModels = new ArrayList<>();
MyModel mymodel = new MyModel();
mymodel.setSomeParam("");
myModels.add(mymodel);
dao.myMethod(myModels);
}
}
While running this test method , i am getting NullPointerException in called method(myMethod()). int[] result is coming as null. My understanding is it should get the result from the stub in the mock.
Please help me understand what am i doing wrong.
It seems that you're not using the correct import for any() because if it would be the correct ArgumentMatchers.any() from Mockito, Mockito would complain that you don't use an ArgumentMatcher for both parameters of .batchUpdate().
You can statically import it with import static org.mockito.ArgumentMatchers.*; or use ArgumentMatchers.any().
So as first step, try the following:
when(namedParameterJdbcTemplate.batchUpdate(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(rowsAffected);
or be less generic and match the return type of SqlParameterSourceUtils.createBatch() with:
// I don't know what SqlParameterSourceUtils.createBatch() returns, so you might have to adjust it
when(namedParameterJdbcTemplate.batchUpdate(ArgumentMatchers.any(), ArgumentMatchers.eq("SOME RETURN"))).thenReturn(rowsAffected);
It worked by adding the cast to the Argument matchers:
Updated Code :
when(namedParameterJdbcTemplate.batchUpdate(anyString(), (SqlParameterSource[]) any())).thenReturn(rowsAffected);

Mockito mock CassandraOperation Slice method

I am using the Mockito framework to write the mock Junit test cases below is my Service class to test the Junit test code
public class UserService {
#Autowired
private CassandraOperations template
public UserDTO getUserDTO(String date, String pagingState) {
Select select = QueryBuilder.select().all()
.from("tbl_user");
select.where(QueryBuilder.eq(date, date));
select.setFetchSize(30);
if (pagingState != null) {
select.setPagingState(PagingState.fromString(pagingState));
}
Slice<UserDTO> usgerDTO = template.slice(select, UserDTO.class);
if(usgerDTO.hasNext()) {
}
return usgerDTO.get();
}
Test case Class is writted
#RunWith(MockitoJUnitRunner.class)
public class UserServiceTest {
#InjectMocks
private UserService service ;
#Mock
private CassandraOperations template;
#Mock
private UserDTO userDTO;
private String date= "2019-09";
#Test(expected = Exception.class)
public void test() {
Slice<UserDTO> userDTO= Mockito.mock(Slice.class);
Select select = Mockito.mock(Select.class);
Mockito.when(template.slice(select, UserDTO.class)).thenReturn(userDTO);
metricReportDTO = service.getUserDTO(date, null);
}
}
I am getting Null values on this line
Slice<UserDTO> usgerDTO = template.slice(select, UserDTO.class);
Can you please helm me how to test template.slice method
Note: I should not use the PowerMockito
Your code snippet doesn't make sense in that you call service.getMetricsReports(date, null) in your test but your UserService code example only has the method getUserDTO. Assuming that is just a typo then you need to use matchers to get the mock to work in your when.
Mockito.when(template.slice(any(Select.class), eq(UserDTO.class))).thenReturn(userDTO);
This means whenever the first param is a class of type Select and the second is equal to UserDTO.class then return the userDTO if those 2 params don't match those conditions it will return null.
You can also make it match the exact Select assuming it has an overridden equals method. In the case where you send a null pagingState then something like:
Select stubSelect = QueryBuilder.select().all()
.from("tbl_user");
stubSelect.where(QueryBuilder.eq(date, date));
stubSelect.setFetchSize(30);
Mockito.when(template.slice(eq(stubSelect), eq(UserDTO.class))).thenReturn(userDTO);

Value of doReturn not used

I use spring boot 2.
I search to test a private method in the facade
#RunWith(SpringRunner.class)
#SpringBootTest
public class SamplingsFacadeTest {
#Autowired
private SamplingsFacade facade;
#MockBean
private SamplingsService samplingsService;
#Test
public void exampleTest() throws Exception {
List<Samplings> samplings = new ArrayList<>();
Samplling sampling = new Samplings();
..
samplings.add(sampling);
//fake call
Mockito.doReturn(samplings).when(samplingsService.getSamplingContainingNonCompliantTest());
//real call
List<FactoryEmailNCDto> factoryEmails = Whitebox.invokeMethod(facade, "prepareDataNoncompliantSampling");
}
public List<Samplings> getSamplingContainingNonCompliantTest() {
return samplingsRepository.findSamplingContainingNonCompliantTest();
}
In Facade In
private List<FactoryEmailNCDto> prepareDataNoncompliantSampling() {
List<FactoryEmailNCDto> factoryEmailNC = new ArrayList<>();
List<Samplings> samplings = samplingsService.getSamplingContainingNonCompliantTest();
for (Samplings sampling : samplings) {
...
}
}
Why when I debug, samplings is null and not the value I created
Mockito.doReturn(samplings)
.when(samplingsService.getSamplingContainingNonCompliantTest());
One potential problem is that doReturn takes the form doReturn(value).when(mock).method(), not doReturn(value).when(mock.method()). The latter is considered an incomplete stubbing. Instead, it should look like this:
Mockito.doReturn(samplings)
.when(samplingsService)
.getSamplingContainingNonCompliantTest();
Note that there may be other problems with your test; the code you've written expects samplingsService to be public and non-final, and your getSamplingContainingNonCompliantTest() to likewise be public, non-static, and non-final, but the code sample you have does not confirm that. You may want to call Mockito.validateMockitoUsage() in an #After method, or use a #Rule that handles Mockito annotations and cleanup for you.

Why is mockito's when().thenReturn() not working?

I am testing JdbcSpitterDao#getSpitterByid() and am mocking jdbcTemplate.queryForObject()
public class JdbcSpitterDao extends JdbcDaoSupport implements SpitterDao {
...
public Spitter getSpitterById(long id,
ParameterizedRowMapper<Spitter> parameterizedRowMapper) {
JdbcTemplate jdbcTemplate = getJdbcTemplate();
return jdbcTemplate.queryForObject(SQL_SELECT_SPITTER,
parameterizedRowMapper, id);
}
...
}
My test object contains the following
ParameterizedRowMapper<Spitter> parameterizedRowMapper = new ParameterizedRowMapper<Spitter>() {
public Spitter mapRow(ResultSet rs, int rowNum) throws SQLException {
Spitter Spitter = new Spitter();
spitter.setId(rs.getLong(1));
spitter.setUsername(rs.getString(2));
spitter.setPassword(rs.getString(3));
spitter.setFullName(rs.getString(4));
spitter.setEmail(rs.getString(5));
return spitter;
}
};
JdbcTemplate jdbcTemplate = mock(JdbcTemplate.class);
JdbcSpitterDao jdbcSpitterDao = new JdbcSpitterDao();
jdbcSpitterDao.setJdbcTemplate(jdbcTemplate);
// Don't connect to the database. Mock JdbcTemplate class
when(
jdbcTemplate.queryForObject(
JdbcSpitterDao.SQL_SELECT_SPITTER,
parameterizedRowMapper, 1)).thenReturn(new Spitter(1, "rajkumarm", "rajmukarm",
"Rajkumar Masaniayan", "rajkumarm#gmail.com"));
// Actual test
Spitter actualSpitter = jdbcSpitterDao.getSpitterById(1, parameterizedRowMapper);
But when().thenReturn() idiom is not getting invoked. The control is flowing to actual jdbcTemplate. Looks like there is a problem with
JdbcTemplate jdbcTemplate = mock(JdbcTemplate.class);
or
jdbcSpitterDao.setJdbcTemplate(jdbcTemplate);
Would you please let me know what is going wrong?
There is this suggestion not to mock the types you don't own. In general it is a good suggestion and here it really makes sense not to unit test that dao at all. Why not just integration test sth which operates on DB and does no additional logic?

Resources