### Question:
CacheBase implements ApplicationContextAware { protected Method getKeyMethod(final Object keyObject) throws NoSuchMethodException { final Method storedMethod = methodStore.find(keyObject.getClass()); if (storedMethod != null) { return storedMethod; } final Method[] methods = keyObject.getClass().getDeclaredMethods(); Method targetMethod = null; for (final Method method : methods) { if (method != null && method.getAnnotation(CacheKeyMethod.class) != null) { if (method.getParameterTypes().length > 0) { throw new InvalidAnnotationException(String.format( "Method [%s] must have 0 arguments to be annotated with [%s]", method.toString(), CacheKeyMethod.class.getName())); } if (!String.class.equals(method.getReturnType())) { throw new InvalidAnnotationException(String.format( "Method [%s] must return a String to be annotated with [%s]", method.toString(), CacheKeyMethod.class.getName())); } if (targetMethod != null) { throw new InvalidAnnotationException(String.format( "Class [%s] should have only one method annotated with [%s]. See [%s] and [%s]", keyObject.getClass().getName(), CacheKeyMethod.class.getName(), targetMethod.getName(), method.getName())); } targetMethod = method; } } if (targetMethod == null) { targetMethod = keyObject.getClass().getMethod("toString", null); } methodStore.add(keyObject.getClass(), targetMethod); return targetMethod; }  void setApplicationContext(ApplicationContext applicationContext); void setCache(MemcachedClientIF cache); void setMethodStore(CacheKeyMethodStore methodStore); void setDefaultKeyProvider(KeyProvider defaultKeyProvider);  }### Answer:
@Test public void testKeyMethodArgs() throws Exception { try { cut.getKeyMethod(new KeyObject01()); fail("Expected exception."); } catch (InvalidAnnotationException ex) { assertTrue(ex.getMessage().indexOf("0 arguments") != -1); System.out.println(ex.getMessage()); } try { cut.getKeyMethod(new KeyObject02()); fail("Expected exception."); } catch (InvalidAnnotationException ex) { assertTrue(ex.getMessage().indexOf("String") != -1); System.out.println(ex.getMessage()); } try { cut.getKeyMethod(new KeyObject03()); fail("Expected exception."); } catch (InvalidAnnotationException ex) { assertTrue(ex.getMessage().indexOf("String") != -1); System.out.println(ex.getMessage()); } try { cut.getKeyMethod(new KeyObject04()); fail("Expected exception."); } catch (InvalidAnnotationException ex) { assertTrue(ex.getMessage().indexOf("only one method") != -1); System.out.println(ex.getMessage()); } assertEquals("doIt", cut.getKeyMethod(new KeyObject05()).getName()); assertEquals("toString", cut.getKeyMethod(new KeyObject06(null)).getName()); }