0

I want to write a test method for a service method. In this service method we are calling a query that fetch the data from database and that value is used in the function to do some processing. I want to stub only this db call. here is my function

public arraylist retrieveSomthing(JdbcTemplate){

//some processing is happening

List<Map<String,Object>> result = JdbcTemplate.queryForList("QueryName");

//some processing is happening for the result return from the query.


}

I want to write the test for the above function but I want to stub only the Jdbc.queryforList statement.

Please help me How to go stub the statement.

Thanks in Advance.

Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
  • Possible duplicate of [Mocking static methods with Mockito](http://stackoverflow.com/questions/21105403/mocking-static-methods-with-mockito) – SpaceTrucker Oct 07 '16 at 09:37

2 Answers2

0
 @Test
 public void testRetrieveSomthing() {
  JdbcTemplate jdbcTemplate = mock(JdbcTemplate.class);
  List < .. > results = ///something you would do dummy return
  when(jdbcTemplate.queryForList(anyString())).thenReturn(results);
  List < .. > alist = instance.retrieveSomthing(jdbcTemplate);
  assertEquals(alist, expectedList);

 }
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
0

It's easy to do

1) Mock the jdbctemplate object like below:

@Mock private JdbcTemplate jdbcTemplate;

2) Since jdbcTemplate.queryForList("QueryName") returns list, make sure that you have created a List object and returning that value while stubbing (like below)

    List<Example> result = new ArrayList();
     result.add("dummy values");
     result.add("dummy values");

then

    //import static org.mockito.Matchers.anyString;
    Mockito.when(jdbcTemplate.queryForList(anyString()).thenReturn(result);

PS: Points to remember while mocking and stubbing are,

1) while dealing with List objects create new object and return as on-stubbing value of actual test class

2) Use matchers like anyString() instead of using some text.

Hope it is useful.

Praveen Kumar Mekala
  • 628
  • 1
  • 10
  • 26
  • Thanks for the comment. But I did exactly what you mentioned but always I am getting null as result. here is my test function – Abhishek Kumar Maurya Oct 08 '16 at 06:27
  • public void retrieveRestrictedUpcsTestEquals(){ ArrayList upcsList=new ArrayList(); upcsList.add(TestDataConstants.UPC1); List> result = new ArrayList>() Map upc_nbr=new HashMap(); upc_nbr.put("upc_nbr", 2) result.add(upc_nbr); Mockito.when(wmJdbcTemplate.queryForList(FETCH_RSTRCTED_UPC_QRY,upcsList)).thenReturn(result); ArrayList upcList =daoImpl.retrieveRestrictedUpcs(wmJdbcTemplate); assertTrue(upcList.size()==2); – Abhishek Kumar Maurya Oct 08 '16 at 06:34
  • I tried with the matchers as well but getting the same result. not sure If I am doing anything wrong. Please help. – Abhishek Kumar Maurya Oct 08 '16 at 06:51
  • @Test public void retrieveRestrictedUpcsTestEquals(){ List> result = new ArrayList>(); Map upc_nbr=new HashMap(); upc_nbr.put("upc_nbr", 2); result.add(upc_nbr); upc_nbr=new HashMap(); upc_nbr.put("upc_nbr", 3); result.add(upc_nbr); Mockito.when(wmJdbcTemplate.queryForList(Mockito.anyString(),Mockito.anyCollection())).thenReturn(result); ArrayList upcList = daoImpl.retrieveRestrictedUpcs(wmJdbcTemplate); assertTrue(upcList.size()==2); } – Abhishek Kumar Maurya Oct 08 '16 at 06:52
  • Are you getting null as result or NullPointerException, if it's exception could you paste here that would be easy to help you. – Praveen Kumar Mekala Oct 10 '16 at 05:35
  • Exceptions -> java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86) at org.junit.Assert.assertTrue(Assert.java:41) at org.junit.Assert.assertTrue(Assert.java:52) at com.walmart.ecommerce.restriction.dao.impl.test.RestrictDaoImplTest.retrieveRestrictedUpcsTestEquals(RestrictDaoImplTest.java:102) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43at java.lang.reflect.Method.invoke(Method.java:498) – Abhishek Kumar Maurya Oct 10 '16 at 07:08
  • Yes I am not getting the desired result that I am trying to mock – Abhishek Kumar Maurya Oct 10 '16 at 07:08