PropertyResolver implements ParameterResolver<Property> { @Override public Object resolve(Parameter parameter, ApplicationContext context) throws Exception { if (parameter.getParameterClass() != String.class) { throw new IllegalArgumentException("Only parameters and fields with string type may be annotated by @Property."); } String param = this.property.value(); Object value = context.getInitialProperties().getProperties().get(param); if (value == null) { return parameter.getDefaultValue(); } return value; } PropertyResolver(Property property); @Override Object resolve(Parameter parameter, ApplicationContext context);  }
@Test public void retrievesPropertyFromApplicationContext() throws Exception { Object resolvedObject = propertyResolver.resolve(parameter, applicationContext); assertEquals("hello", resolvedObject); }
@Test public void retrievesDefaultValueWhenPropertyDoesNotPresentInApplicationContext() throws Exception { when(applicationContext.getInitialProperties().getProperties()).thenReturn(emptyMap()); when(parameter.getDefaultValue()).thenReturn("default"); Object resolvedHeader = propertyResolver.resolve(parameter, applicationContext); assertEquals("default", resolvedHeader); }
@Test public void throwsExceptionWhenPropertyAnnotationIsAppliedToParameterOtherThanString() throws Exception { when(parameter.getParameterClass()).thenReturn((Class)Object.class); thrown.expect(IllegalArgumentException.class); propertyResolver.resolve(parameter, applicationContext); }