设置重写为true后仍接收BeanDefinitionOverrideException

时间:2019-03-04 10:00:20

标签: spring-test

在构建项目时,我的测试错误。我在Google上搜索了一下,发现每个人都在谈论spring如何要求将Bean定义重写设置为true。我的属性已经将Bean覆盖设置为true,但仍然收到此错误。有人知道我在想什么吗?

控制器测试

@WebMvcTest(controllers = PermitController.class)
public class PermitControllerTest extends CrudControllerTest<Permit, PermitRepository> {

    @TestConfiguration
    static class Configuration {
        @Bean
        public PermitController permitController() {
            return new PermitController();
        }
    }

    @Autowired
    private PermitController permitController;

    @MockBean
    private PermitRepository permitRepository;

    @MockBean
    private AccountRepository accountRepository;

    @MockBean
    private PocRepository pocRepository;

    @MockBean
    private DateslotRepository dateslotRepository;

    @MockBean
    private TimeslotRepository timeslotRepository;

    @MockBean
    private CoordinateRepository coordinateRepository;

    // TODO: test invalid fields

    /**
     * Setup before unit tests are run.
     */
    @Before
    public void before() {
        permitController.setRepository(permitRepository);
        super.init(permitRepository);
        super.setUrl(WebConfig.PERMIT + "/");
        super.setEntities(TestEntities.getPermit(null), TestEntities.getPermit(TestEntities.ID1));
    }
}

测试错误

Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'permitController' defined in com.example.permitservice.controller.PermitControllerTest$Configuration: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=permitControllerTest.Configuration; factoryMethodName=permitController; initMethodName=null; destroyMethodName=(inferred); defined in com.example.permitservice.controller.PermitControllerTest$Configuration] for bean 'permitController': There is already [Generic bean: class [com.example.permitservice.controller.PermitController]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [E:\workspace\gitpermitmanager\permitMgrService\build\classes\java\main\com\example\permitservice\controller\PermitController.class]] bound.
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:894)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:274)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:141)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:117)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:327)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:691)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:528)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:127)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
    ... 50 more

属性:

spring.profiles.active = development
spring.main.allow-bean-definition-overriding = true

0 个答案:

没有答案