首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用反射测试内部私有类的方法

利用反射测试内部私有类的方法
EN

Stack Overflow用户
提问于 2016-06-22 18:30:09
回答 4查看 2.3K关注 0票数 2

是否有一种方法可以使用反射测试私有内部类的方法?在下面的代码中,我们如何测试func-1和func-2?

代码语言:javascript
复制
public class Outer extends AbstractOuter {
private final Properties properties;

public Outer(Properties properties) {
    this.properties = properties;
}

private class Inner extends AbstractInner {

    private int numOfProperties;

    @Override
    void func-1() throws Exception {
        //
    }

    private int func-2(long l)  {
        //
    }
}
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-07-12 22:47:46

代码语言:javascript
复制
package SalesUnitsIntoCarton.mySolution;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class UtilityReflections {
    private static final Object[] EMPTY = {};

    /**
     * This method returns a value, returned by the method of a private inner class, of a given object instance.
     *
     * @param outerClassInstance This parameter needs to be an instance of the outer class that contains the private inner class.
     * @param attributeNameOfInnerClassInOuterClass This is the name of the attribute that is an inner class type, within the outer class.
     * @param innerClassName This is the class name of the inner class.
     * @param methodNameOfInnerClass This is the name of the method inside the inner class that should be called.
     * @return Returns the value returned by the method of the inner class. CAUTION: needs casting since its of type {@link Object}
     * 
     * @throws SecurityException 
     * @throws NoSuchFieldException 
     * @throws ClassNotFoundException 
     * @throws NoSuchMethodException 
     * @throws InvocationTargetException 
     * @throws IllegalArgumentException 
     * @throws IllegalAccessException 
     * @throws Exception
     */
     public static <T extends Object> Object executeInnerClassMethod(T outerClassInstance, String attributeNameOfInnerClassInOuterClass, String innerClassName, String methodNameOfInnerClass) 
         throws NoSuchFieldException, SecurityException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException,
         IllegalArgumentException, InvocationTargetException {

         final Class<?> outerClass = outerClassInstance.getClass();

         final Field field = outerClass.getDeclaredField(attributeNameOfInnerClassInOuterClass);
         field.setAccessible(true);

         Class<?> innerClass = Class.forName(innerClassName);
         innerClass = field.getType();

         //access the method
         final Method method = innerClass.getDeclaredMethod(methodName, new Class<?>[]{});
         method.setAccessible(true);
         return method.invoke(field.get(outerClassInstance), EMPTY);
     }
}
票数 1
EN

Stack Overflow用户

发布于 2016-06-22 18:43:36

通过反射和使用setAccessible(true),这是可能的,但这将是困难的,特别是当您有私有的非静态内部类时。

最有趣的问题是:,你为什么要这么做?

这些内部类和方法应该影响被测试的外部类的行为。所以请测试一下那堂课!

尝试测试类的私有部分主要是延迟测试的信号,因为您可能需要更少的设置,您有没有测试的遗留代码,并且只想测试我的更改。但对不起,那是没用的。

  • 您的测试是绑定到实现的白盒测试,而不是类的api。
  • 任何内部重构都可能破坏测试,即使整体行为没有改变。
  • 最重要的是:您不需要测试内部类或私有方法是否使用正确的方法(或者根本不正确)。

这样做的所有努力都是无用的,相反,测试完整的类!

票数 0
EN

Stack Overflow用户

发布于 2016-06-22 19:00:57

我没必要处理这件事,但我认为这是个好问题。我不能百分之百地肯定这件事,但也许这会奏效。

代码语言:javascript
复制
        Outer.class.getDeclaredClasses()[0].getDeclaredMethod("func-1").invoke(null);

我目前没有环境来测试它。但也许会有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37975725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档