Java test class with many private methods

前端 未结 4 581
终归单人心
终归单人心 2021-01-11 17:42

I have a class that has the responsibility of importing contracts from a CSV to database.

The class itself has only one public method that starts the import and the

4条回答
  •  一整个雨季
    2021-01-11 18:22

    You can use reflection to achieve this. The Method class has a method called setAcessible(boolean) which enables you to call it even if declared as private/protected/default. See the example below:

    YourClass yourClassInstance = new YourClass();
    Method yourMethod = YourClass.class.getDeclaredMethod("yourMethod", null);
    yourMethod.setAccessible(true);
    Object[] parameters = new Object[1];
    parameters[0] = "A String parameter";
    Object result = yourMethod.invoke(yourClassInstance, parameters);
    

提交回复
热议问题