Gernot
Gernot

Reputation: 384

SpringBoot unit test does not use @EnableAutoConfiguration annotation from Application class

I want to configure a Spring Boot Application so that no DB is used at all. So I have annotated my Application class for excluding JPA autoconfig classes:

@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class Application {

    public static void main(final String... args) {
        run(Application.class, args);
    }
}

This works fine when the service is run standalone

Unfortunately my test classe seems to ignore the annotation, although I use the Application class for my test

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SwaggerJsonExistenceTest {
    ...
}

The test fails with the following error message

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Cannot determine embedded database for tests. If you want an embedded database please put a supported one on the classpath.

Update: There are no DB drivers on the classpath.

org.springframework.boot:spring-boot-starter-data-jpa is used for testing (included via testCompile directive in gradle)

How has the test to be configured so that it does not use db-related autoconfiguration?

Fix: I have removed all jpa starter dependencies (as no DB is needed), so that datasource autoconfig is not done at all.

Upvotes: 7

Views: 12166

Answers (2)

Steve Gelman
Steve Gelman

Reputation: 932

I'm had this issue with version 2.0.9.RELEASE, which I have to use to stay in compliance with my company standards. What worked for me was:

@SpringBootTest(properties="spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration")

I can't say for certain if this is an issue with @EnableAutoConfiguration in my version of Springboot, but with version 2.1.5.RELEASE, I do not have to set the property with the @SpringBootTest to make this work.

Upvotes: -1

Florian Schmitt
Florian Schmitt

Reputation: 753

The @SpringBootApplication annotation has an exclude property, which you should use in favor of @EnableAutoConfiguration(exclude = ...) in this case. If you use it, the @SpringBootTest annotated tests should enforce the exclude correctly.

Spring Boot API - SpringBootApplication

Upvotes: 0

Related Questions