### Question:
ResourceBundleMessageProducer implements MessageProducer { public ValidationMessage getMessage(InvalidValue error) { Locale locale = bundle.getLocale(); if (locale == null) { locale = Locale.getDefault(); } String key = messageKeyFor(locale, error.getClass(), error); String message; try { message = bundle.getString(key); } catch (MissingResourceException ex) { message = key.replaceFirst("[.]", " : ").replaceAll("_", " "); } return new SimpleValidationMessage(message); } ResourceBundleMessageProducer(final ResourceBundle bundle); ValidationMessage getMessage(InvalidValue error); String messageKeyFor(Locale locale, Class<? extends InvalidValue> errorClass, InvalidValue error);  }### Answer:
@Test public void shouldUseEnumNameLowerCasedAsKey() { final String errorMessage = "message for ANY error"; ResourceBundle bundleMock = new ResourceBundle() { protected Object handleGetObject(final String s) { String errorKey = (MessageFormat.format("{0}.{1}", Errors.class.getSimpleName(), Errors.WITH_COMPOSITE_NAME.name())).toLowerCase(); if (s.equals(errorKey)) { return errorMessage; } return null; } public Enumeration<String> getKeys() { return null; } public Locale getLocale() { return Locale.getDefault(); } }; ResourceBundleMessageProducer producer = new ResourceBundleMessageProducer(bundleMock); ValidationMessage validationMessage = producer.getMessage(Errors.WITH_COMPOSITE_NAME); assertEquals(errorMessage, validationMessage.getMessage()); }
@Test public void shouldReturnMessageWhenKeyIsMissingInResourceBundle() { ResourceBundle bundleMock = new ResourceBundle() { protected Object handleGetObject(String s) { throw new MissingResourceException(null, null, null); } public Enumeration<String> getKeys() { return null; } public Locale getLocale() { return Locale.getDefault(); } }; ResourceBundleMessageProducer producer = new ResourceBundleMessageProducer(bundleMock); ValidationMessage validationMessage = producer.getMessage(Errors.WITH_COMPOSITE_NAME); assertEquals("errors : with composite name", validationMessage.getMessage()); }