Java 排除Spring引导中的Hibernate不工作

Java 排除Spring引导中的Hibernate不工作,java,spring,hibernate,spring-boot,Java,Spring,Hibernate,Spring Boot,我正在尝试在不休眠的情况下启动JUnit测试。我的申请类别: @SpringBootApplication(exclude = { HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class, DataSourceAutoConfiguration.class, JpaBaseConfiguration.class, WebMvcAutoConfiguration.class }) @Compon

我正在尝试在不休眠的情况下启动JUnit测试。我的申请类别:

@SpringBootApplication(exclude = { HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class,
    DataSourceAutoConfiguration.class, JpaBaseConfiguration.class, WebMvcAutoConfiguration.class })
@ComponentScan("my.base.package")
public class TestContext {

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

}
正如你所见,我排除了越来越多的东西,但它总是相同的错误。我的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestContext.class)
public class TestMail {
    @Autowired
    private Component c;

    public void setC(Component c) {
        this.c = c;
    }

    @Test
    public void test() {
       ...
    }

}
当我启动测试时,将配置Hibernate,并在以下情况下失败:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "dev" are currently active).
因此,仅启动应用程序或作为测试上下文也会启动hibernate配置。我做错了什么

编辑: 只有:

@SpringBootApplication(exclude = { HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class}) 
在我尝试使用实际的hibernate工具之前,不会抛出任何异常。但是它正确地构建了数据库

编辑2:
问题是一个自定义配置类上有@EnableJpaRepositories。此外,这似乎不可能,因为带有自动连线存储库的@Component注释的bean将失败,因为无法创建这些bean。

尝试将DataSourceTransactionManagerAutoConfiguration.class添加到排除列表中。当我需要将hibernate从自动配置中排除时,这对我来说很有用


您是否尝试过使用--debug开关启动测试?这将报告自动配置()。

据我所知,您应该在没有Spring上下文的情况下进行单元测试,并使用它进行集成测试。集成测试应启用Hibernate,但应以适当的方式进行配置(即指向内存中的数据库)。这几点在文档部分中都有详细介绍。所以你想做的(两者的混合)对我来说没什么意义,IMHO。实际上还有很多组件不依赖于数据库。事实上,依赖于它的部分非常小,但所有测试都会启动一个数据库,当它们不依赖它时,它们不应该启动该数据库。但我需要应用程序上下文,因为它有许多独立的配置,其中有几个组件。它不适合我。而且--debug开关似乎没有给我任何关于为什么或者配置了什么的特殊输出。我知道get
org.springframework.beans.factory.BeanCreationException:创建名为“jpaMappingContext”的bean时出错:调用init方法失败;嵌套异常为java.lang.IllegalArgumentException:必须至少存在一个JPA元模型