简体   繁体   中英

Exclude Hibernate in Spring Boot not working

I'm trying to start JUnit tests wihout hibernate. My Application class:

@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);
  }

}

As you can see you I excluded more and more stuff but its always the same error. My Tests:

@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() {
       ...
    }

}

When I start the tests Hibernate will be configured and fails with:

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).

So starting only the application or as context for the tests also starts hibernate configuration. What am I doing wrong?

EDIT: Having only:

@SpringBootApplication(exclude = { HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class}) 

Does not throw any exception until I try to use actual hibernate stuff. However it builds the database correctly...

EDIT2: The problem was a custom config class that had @EnableJpaRepositories on. Also this seems not possible because Beans annotated with @Component which have autowired repositories will fail because the beans cannot be created.

Try adding DataSourceTransactionManagerAutoConfiguration.class to the exclude list. This did the trick for me when I needed to exclude hibernate from the auto config.

Have you tried starting up the test with the --debug switch? That would report the auto-configuration ( https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM