How to make instance of CrudRepository interface during testing in Spring?

后端 未结 4 993
旧时难觅i
旧时难觅i 2021-02-07 08:53

I have a Spring application and in it I do not use xml configuration, only Java Config. Everything is OK, but when I try to test it I faced problems wi

4条回答
  •  时光说笑
    2021-02-07 09:18

    What you need to do is:

    1. remove @Repository from ArticleRepository

    2. add @EnableJpaRepositories to PagesTestConfiguration.java

      @Configuration
      @ComponentScan(basePackages = {"com.example.core"}) // are you sure you wanna scan all the packages?
      @EnableJpaRepositories(basePackageClasses = ArticleRepository.class) // assuming you have all the spring data repo in the same package.
      public class PagesTestConfiguration {
      
      @Bean
      public ArticleServiceImpl articleServiceImpl() {
          ArticleServiceImpl articleServiceImpl = new ArticleServiceImpl();
          return articleServiceImpl;
      }
      }
      

提交回复
热议问题