### Question:
ToSpyReplacingProcessor extends InstantiationAwareBeanPostProcessorAdapter { @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { final Class<?> beanClass = resolveBeanClass(bean); final DoubleSearch spySearch = doubleRegistry.spySearch(); if(spySearch.containsAnyDoubleMatching(beanName, beanClass)) { if(spySearch.containsExactlyOneDouble(beanName, beanClass)) { final DoubleDefinition spyDefinition = spySearch.findOneDefinition(beanName, beanClass); return createSpy(bean, spyDefinition); } else if(spySearch.containsExactlyOneDouble(beanName)) { final DoubleDefinition spyDefinition = spySearch.findOneDefinition(beanName); return createSpy(bean, spyDefinition); } else if(spySearch.containsExactlyOneDouble(beanClass)) { final DoubleDefinition spyDefinition = spySearch.findOneDefinition(beanClass); if(hasOnlyOneBeanOfClass(spyDefinition.getDoubleClass())) { return createSpy(bean, spyDefinition); } } else { log.log(Level.WARNING, "Found more than one spy matching " + beanClass.getCanonicalName() + " and " + beanName); } } return bean; } ToSpyReplacingProcessor(ApplicationContext applicationContext, DoubleRegistry doubleRegistry, DoubleFactory doubleFactory, DoubleDefinitionsRegistrationContext doubleDefinitionsRegistrationContext); @Override Object postProcessAfterInitialization(Object bean, String beanName); static final String BEAN_NAME; }### Answer:
@Test public void should_match_nothing_when_multiple_spies_registered_and_has_no_exact_spy_match() { final String serviceName = "service"; final Service service = new Service(); final String childServiceName = "childService"; final ChildService childService = new ChildService(); final ToSpyReplacingProcessor postProcessor = new TestContextBuilder() .withBean(serviceName, service) .withBean(childServiceName, childService) .withSpy(doubleDefinition(Service.class)) .withSpy(doubleDefinition(Service.class)) .build() .toSpyReplacingProcessor; postProcessor.postProcessAfterInitialization(service, serviceName); postProcessor.postProcessAfterInitialization(childService, childServiceName); verifyZeroInteractions(doubleFactory); }
@Test public void should_not_spy_on_spy() { final String spyName = "myService"; final Service spy = Mockito.spy(new Service()); final DoubleDefinition spyDefinition = doubleDefinition(Service.class, spyName); final ToSpyReplacingProcessor postProcessor = new TestContextBuilder() .withSpyBean(spyName, spy, spyDefinition) .withSpy(spyDefinition) .build() .toSpyReplacingProcessor; postProcessor.postProcessAfterInitialization(spy, spyName); Mockito.verifyZeroInteractions(doubleFactory); }
@Test public void should_register_spy_replacement_in_registration_context() { final Service service = new Service(); final String serviceName = "service"; final DoubleDefinition definition = DoubleDefinition.builder() .doubleClass(Service.class) .name(serviceName) .build(); final TestContextBuilder.TestContext testContext = new TestContextBuilder() .withBean(serviceName, service) .withSpy(definition) .build(); final ToSpyReplacingProcessor processor = testContext.toSpyReplacingProcessor; processor.postProcessAfterInitialization(service, serviceName); assertTrue(testContext .doubleDefinitionsRegistrationContext .isBeanDefinitionRegisteredForDouble(definition)); }