Search code examples
javaspring-mvcjunitmockitotestcase

How to get values from the @autowired field in a class to test and how to get value from abstract method


I am using Mockito for testing my java classes using Junit. I am new to the Mockito and Junit test cases. I have one class which is having spring configuration. Please see the code snippet below.

 public abstract class AbstractTaskDao implements TaskDao {
        @Autowired
        NamedParameterJdbcTemplate jdbcTemplate;

        @Override
        public void addTask(BlockingQueue<String> queue, LocalDate tdate) {
            String sql = getQuery();
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("tdate", Date.valueOf(tdate));


            ((JdbcTemplate) jdbcTemplate.getJdbcOperations()).setFetchSize(1000);
            jdbcTemplate.query(sql, paramMap,new classA());
        }
        protected abstract String getQuery();
    }

    Below is my test class.

    public class AbstractTaskDaoTest { 
        @Mock NamedParameterJdbcTemplate jdbcTemplate; 
        @Mock JdbcOperations operation;
        BlockingQueue<String> myqueue;
        @Before
         public void setUp() throws IOException {   
           jdbcTemplate=mock(NamedParameterJdbcTemplate.class);         
                when(jdbcTemplate.getJdbcOperations()).thenReturn(operation);       
              //use reflection to inject autowired field.       
       org.springframework.test.util.ReflectionTestUtils.setField(abstratTaskPDao, "jdbcTemplate", jdbcTemplate);

                        }

                    @Test
                    public void testTask() throws InterruptedException{
                        LocalDate tdate=LocalDate.parse("2014-02-23");
                        AbstractTaskDao  abstratTaskDao = Mockito.mock(AbstractTaskDao .class, Mockito.CALLS_REAL_METHODS);
                        System.out.println("\n\t sql-"+abstratTaskDao.getQuery());
                        abstratTaskDao.addTask(myqueue,tdate);
                    }
                }

The above test cases throws NullPointerException at line "((JdbcTemplate) jdbcTemplate.getJdbcOperations()).setFetchSize(1000);"

So in above code I am getting getQuery as null. as its implemented by other classes like class MyclassQuery extends AbstractTaskDao contains implementation of the method getQuery(). But I am new to the world of Mockito and Junit test cases so I am not getting how to give explicit call in testing.

2nd thing is the @Autowired fields here it is jdbcTemplate is getting null.So how will I get the value of this.

Update: I am getting class cast exception for code "((JdbcTemplate) jdbcTemplate.getJdbcOperations()).setFetchSize(1000);" As jdbcTemplate is NamedParameterJdbcTemplate. How to resolve this.

To solve this issue I added below line to code in AbstractTaskDaoTest class.
 @Mock JdbcTemplate jdbcTemp;
 jdbcTemp=mock(JdbcTemplate.class);         
 when(jdbcTemplate.getJdbcOperations()).thenReturn(jdbcTemp);     

Solution

  • Try this code, I have not compile fully so you will need to figure out the last line.

    @Test
    public void testTask() throws InterruptedException {
        //arrange
        LocalDate tdate = LocalDate.parse("2014-02-23");
        AbstractTaskDao abstratTaskDao = Mockito.mock(AbstractTaskDao.class, Mockito.CALLS_REAL_METHODS);
        NamedParameterJdbcTemplate jdbcTemplate = Mockito.mock(NamedParameterJdbcTemplate.class);
        JdbcOperations operations =  Mockito.mock(JdbcOperations.class);
    
        when(jdbcTemplate.getJdbcOperations()).thenReturn(operations);
        when(abstratTaskDao.getQuery()).thenReturn("select * from dual");
    
        //use reflection to inject autowired field.
        org.springframework.test.util.ReflectionTestUtils.setField(abstratTaskDao, "jdbcTemplate", jdbcTemplate);
    
        System.out.println("\n\t sql-"+abstratTaskDao.getQuery());
    
        //act
        abstratTaskDao.addTask(myqueue, tdate);
    
        //assert
        verify(jdbcTemplate).query(...);
    
    }