### Question:
BeanLocator { public <T> T getBean(Class<T> beanClass) throws IllegalArgumentException { Map<String, T> matchingBeansByName = applicationContext.getBeansOfType(beanClass); return getOnlyOneOrThrow(beanClass.getSimpleName(), matchingBeansByName); } BeanLocator(ApplicationContext applicationContext); T getBean(Class<T> beanClass); T getBeanByPartialName(String partialName, Class<T> beanClass);  }### Answer:
@Test void getBean_returns_when_one_bean_matches() { ApplicationContext applicationContext = mock(ApplicationContext.class); BeanLocator beanLocator = new BeanLocator(applicationContext); when(applicationContext.getBeansOfType(String.class)) .thenReturn(Maps.build(String.class, "testBeanName", "testBean")); assertThat(beanLocator.getBean(String.class)).isEqualTo("testBean"); }
@Test void getBean_throws_when_no_bean_matches() { ApplicationContext applicationContext = mock(ApplicationContext.class); BeanLocator beanLocator = new BeanLocator(applicationContext); when(applicationContext.getBeansOfType(String.class)) .thenReturn(Collections.emptyMap()); assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> beanLocator.getBean(String.class)) .withMessage("No bean matching String"); }
@Test void getBean_throws_when_more_than_one_bean_matches() { ApplicationContext applicationContext = mock(ApplicationContext.class); BeanLocator beanLocator = new BeanLocator(applicationContext); when(applicationContext.getBeansOfType(String.class)) .thenReturn(Maps.build(String.class, "testBeanName1", "testBean1", "testBeanName2", "testBean2" )); assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> beanLocator.getBean(String.class)) .withMessage("Multiple beans matching String. Available: testBeanName1, testBeanName2"); }