### Question:
ObjectMessageSourceUtils { public static ObjectMessageSource getObjectMessageSource(MessageSource messageSource) { Assert.notNull(messageSource, "MessageSource must not be null"); return getObjectMessageSource(messageSource, null); }  static ObjectMessageSource getObjectMessageSource(MessageSource messageSource); static ObjectMessageSource getObjectMessageSource(MessageSource messageSource,
			ApplicationContext fallbackApplicationContext);  }### Answer:
@Test public void shouldNeedMessageSourceOrApplicationContext() throws Exception { this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("ApplicationContext must not be null"); ObjectMessageSourceUtils.getObjectMessageSource(null, null); }
@Test public void shouldUseMessageSourceAsObjectMessageSource() throws Exception { ObjectMessageSource messageSource = mock(ObjectMessageSource.class); ObjectMessageSource objectMessageSource = ObjectMessageSourceUtils.getObjectMessageSource(messageSource, null); assertThat(objectMessageSource, is(sameInstance(messageSource))); }
@Test public void shouldWrapMessageSourceInDefaultObjectMessageSource() throws Exception { MessageSource messageSource = mock(MessageSource.class); ObjectMessageSource objectMessageSource = ObjectMessageSourceUtils.getObjectMessageSource(messageSource, null); assertThat(objectMessageSource, is(DefaultObjectMessageSource.class)); objectMessageSource.getMessage(this.code, this.args, this.locale); verify(messageSource).getMessage(this.code, this.args, this.locale); }
@Test public void shouldUseMessageSourceBean() throws Exception { ApplicationContext applicationContext = mock(ApplicationContext.class); ObjectMessageSource messageSource = mock(ObjectMessageSource.class); given(applicationContext.containsBean(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME)).willReturn(true); given(applicationContext.getBean(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME)) .willReturn(messageSource); ObjectMessageSource objectMessageSource = ObjectMessageSourceUtils.getObjectMessageSource(null, applicationContext); assertThat(objectMessageSource, is(sameInstance(messageSource))); }
@Test public void shouldUseApplicationContext() throws Exception { ApplicationContext applicationContext = mock(ApplicationContext.class); ObjectMessageSource objectMessageSource = ObjectMessageSourceUtils.getObjectMessageSource(null, applicationContext); assertThat(objectMessageSource, is(DefaultObjectMessageSource.class)); objectMessageSource.getMessage(this.code, this.args, this.locale); verify(applicationContext).getMessage(this.code, this.args, this.locale); }