### Question:
BeanDefinitionFinder { public Optional<BeanDefinition> tryToFindBeanDefinition(String beanName) { try { return Optional.of(definitionRegistry.getBeanDefinition(beanName)); } catch (NoSuchBeanDefinitionException ex) { return Optional.empty(); } } BeanDefinitionFinder(ConfigurableListableBeanFactory definitionRegistry, DoubleRegistry doubleRegistry);  BeanDefinitionFinder(ApplicationContext applicationContext, DoubleRegistry doubleRegistry); Optional<BeanDefinition> tryToFindBeanDefinition(String beanName); Optional<BeanDefinition> tryToFindBeanDefinition(String beanName, Class<?> doubleClass); Optional<BeanDefinition> tryToFindSingleBeanDefinition(Class<?> doubleClass);  }### Answer:
@Test public void should_find_bean_definition_by_name() { final String beanName = "object"; final ApplicationContext appContext = buildAppContext(Stream.of( bean(beanName, new Object()))); final BeanDefinitionFinder definitionFinder = new BeanDefinitionFinder(appContext, emptyDoubleRegistry()); final Optional<BeanDefinition> definition = definitionFinder.tryToFindBeanDefinition(beanName); assertTrue(definition.isPresent()); }
@Test public void should_fallback_to_find_by_class_search_if_no_name_match() { final ApplicationContext appContext = buildAppContext(Stream.of( bean("string", "any bean"))); final BeanDefinitionFinder definitionFinder = new BeanDefinitionFinder(appContext, emptyDoubleRegistry()); final Optional<BeanDefinition> definition = definitionFinder.tryToFindBeanDefinition( "not matching name", String.class); assertTrue(definition.isPresent()); }
@Test public void should_ignore_existing_spies_when_searching_for_bean_by_class() { final String spyName = "spy"; final DoubleRegistry doubleRegistry = new DoubleRegistry( emptyList(), singletonList(doubleDefinition(String.class, spyName))); final ApplicationContext appCtx = buildAppContext(Stream.of( bean(spyName, spyName + " - bean") )); final BeanDefinitionFinder definitionFinder = new BeanDefinitionFinder(appCtx, doubleRegistry); final Optional<BeanDefinition> definition = definitionFinder.tryToFindBeanDefinition("any bean name", String.class); assertFalse(definition.isPresent()); }
@Test public void should_ignore_existing_mocks_when_searching_for_bean_by_class() { final String mockName = "mock"; final DoubleRegistry doubleRegistry = new DoubleRegistry( singletonList(doubleDefinition(String.class, mockName)), emptyList()); final ApplicationContext appCtx = buildAppContext(Stream.of( bean(mockName, mockName + " - bean") )); final BeanDefinitionFinder definitionFinder = new BeanDefinitionFinder(appCtx, doubleRegistry); final Optional<BeanDefinition> definition = definitionFinder.tryToFindBeanDefinition("any bean name", String.class); assertFalse(definition.isPresent()); }
@Test public void should_resolve_factory_bean_definition() { final String factoryBeanName = "factoryBean"; final DoubleRegistry doubleRegistry = new DoubleRegistry( emptyList(), emptyList()); final ApplicationContext appCtx = buildAppContext(Stream.of( bean(factoryBeanName, new AnyFactoryBean()) )); final BeanDefinitionFinder definitionFinder = new BeanDefinitionFinder(appCtx, doubleRegistry); Optional<BeanDefinition> definition = definitionFinder.tryToFindBeanDefinition(FACTORY_BEAN_PREFIX + factoryBeanName, FactoryBean.class); assertTrue(definition.isPresent()); }