单元测试时如果遇到以下报错信息,有两种方式解决
 org.springframework.beans.factory.BeanCreationNotAllowedException:
 Error creating bean with name 'eurekaAutoServiceRegistration': Singleton bean...

方法一:添加注解

在单元测试类中添加如下注解(放心不会有地方引用,但是不能删除)

@MockBean
private EurekaAutoServiceRegistration eurekaAutoServiceRegistration;

方法二:实现 BeanFactoryPostProcessor接口

@Component
public class FeignBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        BeanDefinition bd = beanFactory.getBeanDefinition("feignContext");
        bd.setDependsOn("eurekaServiceRegistry", "inetUtils");
    }
}

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐