I want to Mock the r2dbcEntityTemplate
and Unit Test the DaoImpl
class.
I have the DaoImpl
class with definition:
public class StudentDaoImpl implements StudentDao{
private final R2dbcEntityTemplate r2dbcEntityTemplate;
@Override
public Mono<Student> upsert(Student student) {
return r2dbcEntityTemplate
.getDatabaseClient()
.sql(DaoUtil.UPSERT_QUERY)
.bind("student_id", student.getStudentId())
.bind("name", student.getName())
.bind("roll", student.getRoll())
.map(DaoUtil.STUDENT_MAPPING_FUNCTION)
.one();
}
}
Dao
Class:
public interface StudentDao {
Mono<Student> upsert(Student student);
}
and DaoUtil
class:
public class DaoUtil{
public static final String UPSERT_QUERY = "INSERT INTO \n "
+ "student \n"
+ " (student_id, \n "
+ " name, \n"
+ " roll) \n"
+ " VALUES \n"
+ " (:student_id, \n"
+ " :name, \n"
+ " :roll) \n"
+ " ON CONFLICT \n"
+ " (student_id, \n"
+ " name) \n"
+ " DO UPDATE SET \n"
+ " roll = \n"
+ " excluded.roll"
+ " RETURNING *;";
public static final Function<Row, Student>
STUDENT_MAPPING_FUNCTION =
row ->
Student.builder()
.id(row.get("id", Long.class))
.studentId(row.get("student_id", String.class))
.name(row.get("name", String.class))
.roll(row.get("roll", String.class))
.build();
}
I am not finding correct Constructors to mock the r2dbcEntityTemplate
Please guide me to a suitable example for this usecause.
Thanks in advance.