MessageUtil { public static String getMessage(ResourceBundle bundle, String key, Object...params) { if (bundle == null || key == null) { return key; } try { return formatMessage(bundle.getString(key), params); } catch (MissingResourceException e) { return key; } }  static String getMessage(ResourceBundle bundle, String key, Object...params); static String formatMessage(String message, Object...params);  }
@Test public void getMessage() { ResourceBundle bundle = ResourceBundle.getBundle(MessageUtilTest.class.getName()); String key = "key"; String notFoundKey = "notFound"; assertNull(MessageUtil.getMessage(null, null, (Object[]) null)); assertNull(MessageUtil.getMessage(bundle, null, (Object[]) null)); assertSame(key, MessageUtil.getMessage(null, key, (Object[]) null)); assertSame(notFoundKey, MessageUtil.getMessage(bundle, notFoundKey, (Object[]) null)); assertEquals("message aa, {1}, {2}, {3}, {4}", MessageUtil.getMessage(bundle, key, "aa")); assertEquals("message aa, bb, {2}, {3}, {4}", MessageUtil.getMessage(bundle, key, "aa", "bb")); assertEquals("message aa, bb, cc, {3}, {4}", MessageUtil.getMessage(bundle, key, "aa", "bb", "cc")); assertEquals("message aa, bb, cc, dd, {4}", MessageUtil.getMessage(bundle, key, "aa", "bb", "cc", "dd")); assertEquals("message aa, bb, cc, dd, ee", MessageUtil.getMessage(bundle, key, "aa", "bb", "cc", "dd", "ee")); }