title: How to make Class.getMethod() throw a SecurityException
body: <p>I have a utility method to find an object's getter method for a particular field (using reflection):</p>
  
  <pre><code>public static Method findGetter(final Object object, final String fieldName) {
      if( object == null ) {
          throw new NullPointerException("object should not be null");
      } else if( fieldName == null ) {
          throw new NullPointerException("fieldName should not be null");
      }
  
      String getterName = getMethodNameForField(GET_PREFIX, fieldName);
  
      Class&lt;?&gt; clazz = object.getClass();
      try {
          return clazz.getMethod(getterName);
      } catch(final NoSuchMethodException e) {
          // exception handling omitted
      } catch(final SecurityException e) {
          // exception handling omitted
      }
  }
  </code></pre>
  
  <p>I would like to write a unit test that covers the <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/SecurityException.html" rel="noreferrer">SecurityException</a> scenario, but how can I make <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getMethod%28java.lang.String,%20java.lang.Class...%29" rel="noreferrer">getMethod</a> throw a SecurityException?</p>
  
  <p>The javadoc states that <code>getMethod</code> will throw SecurityException</p>
  
  <blockquote>
    <p>If a security manager, s, is present and any of the following
    conditions is met:</p>
    
    <ul>
    <li><p>invocation of s.checkMemberAccess(this, Member.PUBLIC) denies access to the method</p></li>
    <li><p>the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of
    s.checkPackageAccess() denies access to the package of this class</p></li>
    </ul>
  </blockquote>
  
  <p>I would prefer to trigger the exception normally, rather than resorting to a mocking framework.</p>