NoSuchBeanDefinitionException用于深度继承

时间:2018-11-30 15:58:57

标签: java unit-testing spring-mvc spring-boot

我有一个Spring Boot应用程序。我的存储库以以下方式编写:

界面:

@NoRepositoryBean
public interface MyQueryRepository {

    int checkThis(String this);

    int checkThisAndThat(String this, String that);

}

抽象类:

public abstract class AbstractMyQueryRepository implements MyQueryRepository {

        @Override
        public int checkThis(String this) {...}

        @Override
        public int checkThisAndThat(String this, String that) {...}

}

然后执行:

@Transactional(TVServiceDbConfigAtlanta.TV_TRANSACTION_MANAGER_NAME_ATLANTA)
@Repository
public class MyQueryRepositoryAtlanta extends AbstractMyQueryRepository {

  @Autowired
  public MyQueryRepositoryAtlanta (
          @Qualifier(TVServiceDbConfigAtlanta.TV_DATA_SOURCE_BEAN_NAME_ATLANTA) final DataSource dataSource) {
    super(dataSource);
  }
}

当我尝试通过以下方式自动连接UT中的MyQueryRepositoryAtlanta时:

@ExtendWith(SpringExtension.class)
@DataJpaTest
@ActiveProfiles("test")
class MyQueryRepositoryAtlantaTest{


    @Autowired
    private MyQueryRepositoryAtlanta myQueryRepositoryAtlanta ;

    @Test
    void checkThisTest() {
        assertThat(myQueryRepositoryAtlanta.checkThis("ABC")).isEqualTo(1);
    }


    @TestConfiguration
    @Profile("test")
    @Import(TVServiceDbConfigAtlanta.class)
    public static class Config {

    }
}

我收到以下错误:

  

org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为bean的错误   'com.myapp.MyQueryRepositoryAtlantaTest':不满意的依赖关系   通过字段'myQueryRepositoryAtlanta'表示;嵌套异常   是org.springframework.beans.factory.NoSuchBeanDefinitionException:否   类型为com.myapp.AbstractMyQueryRepository的合格Bean   可用:至少有1个符合自动装配条件的bean   候选人。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(required = true)}     在   org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586)   〜[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]在   org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)   〜[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]在   org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372)   〜[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1341)   〜[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:393)   〜[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]在   org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)   〜[spring-test-5.0.8.RELEASE.jar:5.0.8.RELEASE] [...]

由于以下原因,我不确定我是否理解原因:

  • 我正在测试的bean被标注为@Repository和
  • 没有其他以相同方式调用的bean

我还在SOF中检查了类似的答案,但由于我使用的特定结构,它们不适用。关于如何解决此问题的任何想法吗?

0 个答案:

没有答案