测试私有方法

public abstract class ExtensibleTestCase extends UnitilsJUnit4
{
    protected Object CallByName( Object object, String method, Object... args )
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException, NoSuchMethodException
    {
        int argsCount = args.length;
        
        for ( Method m : object.getClass().getDeclaredMethods() )
        {
            if ( m.getName().equals( method )&&argsCount == m.getParameterTypes().length )
            {
                for (int i=0;i<args.length;i++)
                {
                    if (!m.getParameterTypes()[i].isInstance(args[i] ))
                        throw new NoSuchMethodException( method);
                }
                m.setAccessible( true );
                return m.invoke( object, args );

            }

        }
        throw new NoSuchMethodException( method);
    }
}

你可能感兴趣的:(测试)