0

How do i test the repository class which uses JdbcTemplate and SimpleJdbcCall as below

This is what i have tried

Below is the test code and repository code I am using Spring boot latest version junit 5 and mockito. I have tried looking into the below solution but could got it working

Mockito for SimpleJdbcCall with Spring JdbcTemplate

@RunWith(MockitoJUnitRunner.class)
public class DataRepositoryTest {

    
    @InjectMocks
    private DataRepository dataRepository 
    
    
    @Mock
    private JdbcTemplate oracleJDBCTemplate;
    
     @Mock
     private SimpleJdbcCall simpleJdbcCall;
    
    
    
    @BeforeEach
    public void setup() {
        
        MockitoAnnotations.initMocks(this);
        
        simpleJdbcCall = new SimpleJdbcCall(oracleJDBCTemplate)
                .withProcedureName("PROC_NAME_P1")
                .withoutProcedureColumnMetaDataAccess()
                .declareParameters(
                           
                                new SqlParameter("p_in_name", Types.VARCHAR),
                                new SqlParameter("p_age", Types.VARCHAR)
                                
                        
                                ).returningResultSet("ref_cur", new DataRowMapper());
    
        
        

        }
    
    
    @Test
    public void test()
    {
        
        System.out.println(" JdbcTemplate-- "  + oracleJDBCTemplate ); // prints is oracleJDBCTemplate;
        
        System.out.println(" simpleJdbcCall-- "  + simpleJdbcCall ); //print object hascode

    }

}

Below is the repository code @Repository public class DataRepository{

    @Autowired
    @Qualifier("OracleJDBCTemplate")
    private JdbcTemplate oracleJDBCTemplate;

    
    
    private SimpleJdbcCall simpleJdbcCall;
    
    
    @PostConstruct
    public void init()
    {
        simpleJdbcCall = new SimpleJdbcCall(oracleJDBCTemplate)
                .withProcedureName("PROC_NAME_P1")
                .withoutProcedureColumnMetaDataAccess()
                .declareParameters(
                           
                                new SqlParameter("p_in_name", Types.VARCHAR),
                                new SqlParameter("p_age", Types.VARCHAR)
                                
                        
                                ).returningResultSet("ref_cur", new DataRowMapper());
        


                        

                
    }

    @SuppressWarnings("unchecked")
    public List<Person>  getPerson(Data data) {
        

        
        
          Map<String, Object> result =
                  simpleJdbcCall.execute(date.getName,date.getAge);
          

          
          return (List<Person>) result.get("ref_cur");
      }
}
hello world
  • 171
  • 3
  • 13

1 Answers1

1

Seems like you're mixing JUnit 4 and JUnit Jupiter (part of JUnit 5) here.

  • @RunWith is JUnit 4 (JUnit Jupiter equivalent is @ExtendWith(MockitoExtension.class)
  • @BeforeEach is JUnit Jupiter

Furthermore don't manually initialize your simpleJdbcCall when you want to have a mock of it. When writing a unit test for your DataRepository and let Mockito create the class under test for you, @PostConstruct won't be called, so it's fine to mock all collaborators.

The following setup will mock your and you can use Mockito's when().thenReturn() to define the behavior of your mocks:

@ExendWith(MockitoExtension.class)
public class DataRepositoryTest {

    @InjectMocks
    private DataRepository dataRepository 
    
    @Mock
    private JdbcTemplate oracleJDBCTemplate;
    
    @Mock
    private SimpleJdbcCall simpleJdbcCall;

    @Test
    void test() {

    }
}
rieckpil
  • 10,470
  • 3
  • 32
  • 56