java 调用 私有方法

来源:互联网 发布:java生成0到1的随机数 编辑:程序博客网 时间:2024/05/21 21:02
TestPKGLog logObj = log;Method method = null;method = logObj.getClass().getDeclaredMethod("propertiesTOfile", new Class[]{Properties.class});// 要调用的方法propertiesTOfilemethod.setAccessible(true);// 允许处理私有方法Properties prop = new Properties();prop.put("logfile", "songjie");method.invoke(logObj, new Object[]{prop});// 调用方法method.setAccessible(false);

在编写junit中往往无法测试私有函数的逻辑正确性,所以需要通过这种“反射机制”来实现这样的测试工作。


原创粉丝点击