vendredi 7 octobre 2016

How to test java reflections code using JUnit

I have a java class that invoke a method via reflection. That method create database connection and perform database operations. i want to test my reflections code using junit. Is there any way to do that?

Please find my code snippet below.

    Class DaoImpl {

    public void setResult(String result) {
        this.result = result
    }

    public String getResult() {
        return this.result;
    }

    public void doDBoperation() {
        Connection connection = getConnection();
        Statement st = connection.createStatement();
        ResultSet rs = st.executeQuery("Select column_name from table");
        if(rs.next()) {
            result = "value";
        }
    }
}


Class ReflectionClass {

    public void invoke() {
        Class<?> noParam = {};

        Class<?> clazz = Class.forname("DaoImpl");
        Method method = clazz.getDeclaredMethod("doDBoperation", noParam);

        method.invoke(clazz.getNewInstance);
        System.out.println("This is it");
    }
}

How to write JUnit test case for my ReflectionClass?





Aucun commentaire:

Enregistrer un commentaire