Spring @WebMvcTest with @EnableJpa* annotation

前端 未结 1 1752
礼貌的吻别
礼貌的吻别 2021-02-13 04:56

I have the following Main Class.

@EnableJpaAuditing
@SpringBootApplication
@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBea         


        
1条回答
  •  既然无缘
    2021-02-13 05:34

    Move the @Enable* annotations to a dedicated @Configuration class. This is also explained in the Spring Boot Reference Guide in the special section User Configuration and Slicing and noted here.

    Your main class should only have the @SpringBootApplication annotation. Then create a specific @Configuration annotated class with the @Enable stuff on it.

    @SpringBootApplication
    public class SampleApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SampleApplication.class, args);
        }
    }
    

    And a specific configuration for JPA

    @Configuration
    @EnableJpaAuditing
    @EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)
    public class JpaEnversConfiguration {}
    

    Now you can safely use @WebMvcTest without it complaining about JPA.

    0 讨论(0)
提交回复
热议问题