### Question:
ApplicationContextWalker { public boolean hasOnlyOneBeanOfClass(Class<?> beanClass) { return 1 == walkContext(ctx -> ctx.getBeansOfType(beanClass).values()) .stream() .mapToInt(Collection::size) .sum(); } ApplicationContextWalker(ApplicationContext applicationContext); BeanDefinition getBeanDefinition(String beanName); boolean hasOnlyOneBeanOfClass(Class<?> beanClass); Collection<String> getBeanDefinitionNames();  }### Answer:
@Test public void should_detect_when_has_single_bean_of_type() { class Bean1 {} class Bean2 {} final ApplicationContext parentContext = buildAppContext(Stream.of(bean("bean1", new Bean1()))); final ApplicationContext ctx = buildAppContext(parentContext, Stream.of(bean("bean2", new Bean2()))); final ApplicationContextWalker walker = new ApplicationContextWalker(ctx); assertTrue(walker.hasOnlyOneBeanOfClass(Bean1.class)); assertTrue(walker.hasOnlyOneBeanOfClass(Bean2.class)); }
@Test public void should_detect_duplicated_beans_of_the_same_type_on_the_same_level() { class Bean {} final ApplicationContext ctx = buildAppContext(Stream.of( bean("1", new Bean()), bean("2", new Bean()))); final ApplicationContextWalker walker = new ApplicationContextWalker(ctx); assertFalse(walker.hasOnlyOneBeanOfClass(Bean.class)); }
@Test public void should_detect_duplicated_beans_of_the_same_type_on_different_contexts() { class Bean {} final ApplicationContext parent = buildAppContext(Stream.of(bean("1", new Bean()))); final ApplicationContext ctx = buildAppContext(parent, Stream.of(bean("2", new Bean()))); final ApplicationContextWalker walker = new ApplicationContextWalker(ctx); assertFalse(walker.hasOnlyOneBeanOfClass(Bean.class)); }