Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] new naming scheme generates ~almost always~ unique names from the
caller stack trace

zluspai@gmail.com

2008-07-19


Author LoginPost Reply
Hi,

As recently switching to JMock 2.5 from 1.x have found it strange that
jmock complains about non-unique method names. While I understand that
good mock names are important for identifying the mock, I think most of
the time it would be enough if the caller test-method name would become
automatically part of the mock name.

For example, you have this test method (note line numbers below):

10   // my test method
11   public void testMath() {
12    firstMath = context.mock(Math.class);
13    secondMath = context.mock(Math.class);
14    ...more...
15   }

Then using the following naming scheme you will automatically get the
math(testMath:12) and math(testMath:13) generated names for firstMath
and secondMath mocks. Feel free to use this:
Cheers,
Zoltan

  /**
  * Mock object naming scheme generates unique names, by putting the
caller's stack trace in the name.
  */
  public class UniqueStackTraceMethodNamingScheme implements
MockObjectNamingScheme {
   
    private MockObjectNamingScheme delegate;

    /**
     * Default constructor, uses {@(protected)
generating the name.
     */
    public UniqueStackTraceMethodNamingScheme() {
       this(new CamelCaseNamingScheme());
    }
   
    /**
     * Constructor with a delegate naming scheme.
     * @param delegate The delegate
     */
    public UniqueStackTraceMethodNamingScheme(MockObjectNamingScheme
delegate) {
       this.delegate = delegate;
    }
   
    /* (non-Javadoc)
     * @see
org.jmock.lib.CamelCaseNamingScheme#defaultNameFor(java.lang.Class)
     */
    public String defaultNameFor(Class<?> typeToMock) {
       String name = delegate.defaultNameFor(typeToMock);
       StackTraceElement st = findCallerStackTraceElement();
       String nameFromStackTrace = st == null ? "" :
          "(" + /*st.getClassName() + "." +*/ st.getMethodName() +
":" + st.getLineNumber() +")";
       return name + nameFromStackTrace;
    }

    /**
     * Find the caller method, which is used for name generation.
     * @return
     */
    protected StackTraceElement findCallerStackTraceElement() {
       StackTraceElement[] stElements = (new
Exception()).getStackTrace();
       StackTraceElement callerStackTraceElem = null;
     
       for (StackTraceElement stackTraceElement : stElements) {
          if (acceptStackTrace(stackTraceElement)) {
            callerStackTraceElem = stackTraceElement;
            break;
          }
       }
       return callerStackTraceElem;
    }

    /**
     * Check if this is the real caller method. Override this to do
custom finding for method name
     * @param element
     * @return
     */
    protected boolean acceptStackTrace(StackTraceElement element) {
       // ignore all stack trace elements on this class
       String cname = this.getClass().getName();
       if (element.getClassName().equals(cname)) {
          return false;
       }
       // ignore all jmock classes
       if (element.getClassName().startsWith("org.jmock")) {
          return false;
       }
       return true;
    }
  }




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.