private String determinePrimaryCandidate(BeanDefinitionRegistry registry,
                                           Collection<String> candidateBeanNames, ResolvableType type) {
    String primaryBeanName = null;
    for (String candidateBeanName : candidateBeanNames) {
      BeanDefinition beanDefinition = registry.getBeanDefinition(candidateBeanName);
      if (beanDefinition.isPrimary()) {
        if (primaryBeanName != null) {
          throw new NoUniqueBeanDefinitionException(type.resolve(),
            candidateBeanNames.size(),
            "more than one 'primary' bean found among candidates: "
              + candidateBeanNames);
        }
        primaryBeanName = candidateBeanName;
      }
    }
    return primaryBeanName;
  }
Factory method used by defined beans to actually create the mock.

@param mockDefinition the mock definition
@param name the bean name
@return the mock instance
spock-spring/src/main/java/org/spockframework/spring/mock/SpockMockPostprocessor.java