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