Skip to content

How to Disable Database Autoconfiguration in Spring Boot Tests Without a Database

Purpose

This post shows how to run Spring Boot tests when you don’t need a database, but still encounter database autoconfiguration errors.

Environment

  • Spring Boot 2.x
  • Java 11
  • Gradle build
  • No database driver on classpath

What Happened?

I’m building a Spring Boot application that doesn’t use any database. It’s a simple web service.

But when I run tests, I get this error:

Test error output
Cannot determine embedded database for tests.
If you want an embedded database please put a supported one on the classpath.

I was confused. I don’t want a database at all. Why is Spring Boot trying to configure one?

How to Solve It?

There are two solutions.

Solution #1: Exclude Autoconfiguration Classes

If you need database in production but not in certain tests, exclude the autoconfiguration classes:

Application.java
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class
})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

The three classes to exclude:

  • DataSourceAutoConfiguration - prevents datasource setup
  • HibernateJpaAutoConfiguration - prevents JPA/Hibernate setup
  • JpaRepositoriesAutoConfiguration - prevents repository scanning

My test now works:

ApiEndpointTest.java
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApiEndpointTest {
@Test
public void testHealthEndpoint() {
// Test runs without datasource configuration
}
}

Solution #2: Remove JPA Starter Dependencies (Cleaner)

If your application truly never needs a database, remove the JPA starter from dependencies entirely.

I checked my build.gradle and found:

build.gradle (before)
dependencies {
testCompile 'org.springframework.boot:spring-boot-starter-data-jpa' // This was the culprit
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile 'org.springframework.boot:spring-boot-starter-web'
}

The spring-boot-starter-data-jpa was included via testCompile. This triggers Spring Boot’s datasource autoconfiguration during tests.

I removed it:

build.gradle (cleaned)
dependencies {
// Removed: testCompile 'org.springframework.boot:spring-boot-starter-data-jpa'
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile 'org.springframework.boot:spring-boot-starter-web'
}

Now tests run without any database configuration errors. No exclusions needed.

The Reason

Spring Boot autoconfiguration works by scanning your classpath.

When it finds spring-boot-starter-data-jpa, it automatically tries to configure:

  • A datasource connection
  • JPA/Hibernate
  • Repository scanning

If no database driver or embedded database is available, it fails.

The key insight: if you don’t need a database, don’t include database-related starters. This is cleaner than adding exclusions.

Common Mistakes

Mistake #1: Including JPA starter “just in case”

build.gradle (bad practice)
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' // Why include if not used?
}

This triggers unwanted autoconfiguration and slows down startup.

Mistake #2: Using @EnableAutoConfiguration(exclude) instead of @SpringBootApplication(exclude)

Application.java (wrong approach)
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) // Tests may ignore this
public class Application { ... }

Tests don’t properly inherit this exclusion. Use the exclude property on @SpringBootApplication directly.

Mistake #3: Forgetting JpaRepositoriesAutoConfiguration

If you exclude datasource but not repository autoconfiguration, Spring still tries to scan for JPA repositories and fails.

Why does the main application work but tests fail?

The main application context and test context are created differently. Tests use @SpringBootTest which has its own context loading mechanism. Exclusion handling can differ between these contexts.

What if I need database in some tests but not others?

Use @TestConfiguration to conditionally enable database for specific tests:

DatabaseTestConfig.java
@TestConfiguration
@EnableJpaRepositories
@Import(DataSourceAutoConfiguration.class)
public class DatabaseTestConfig {
// This configuration enables database for tests that need it
}

Then import it only in tests that need database:

RepositoryTest.java
@SpringBootTest
@Import(DatabaseTestConfig.class)
public class RepositoryTest {
// This test uses database
}

Summary

In this post, I showed how to disable database autoconfiguration in Spring Boot tests. The key point is: if you don’t need a database, remove the JPA starter dependencies. If you need database in production but not in tests, use @SpringBootApplication(exclude = ...) to selectively disable autoconfiguration.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments