Spring Boot 1.4 DataJpaTest - Error creating bean with name 'dataSource'
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Spring Boot 1.4 introduced a variety of testing improvements aimed at simplifying the testing process, but developers occasionally encounter issues, such as the error while creating a bean with name 'dataSource' when using the @DataJpaTest
annotation. Understanding the intricacies of this error can help in troubleshooting and ensuring your tests run smoothly.
Understanding @DataJpaTest
The @DataJpaTest
annotation is part of Spring Boot's testing suite, designed specifically for JPA components. This annotation triggers an in-memory database setup by default and scans only for JPA repositories, which optimizes your test environment by applying configurations pertinent to JPA data access only.
Common Issue: Error Creating Bean with Name 'dataSource'
Root Cause
This error typically occurs because the context failed to create a DataSource
bean. When using @DataJpaTest
, Spring Boot automatically configures an in-memory database like H2 or HSQLDB. If the application is unable to configure the in-memory database, you might encounter this error.
Common Scenarios Leading to this Issue
- Missing In-Memory Database Dependency: Spring Boot 1.4 doesn't automatically include an in-memory database. If your classpath doesn't contain H2, HSQLDB, or Derby, Spring Boot cannot configure a default
DataSource. - **Misconfigured
application.properties**: Custom data source properties can override the default embedded database configuration, potentially resulting in this error. - Wrong Spring Configuration: Conflicting or misconfigured
@Configurationclasses might interfere with default configurations provided by@DataJpaTest. - Multiple DataSources: Defining multiple
DataSourcebeans without a clear primary one can confuse Spring's auto-configuration.
Fixes and Workarounds
Ensure the In-Memory Database Dependency is Present
To resolve the issue, ensure that your project has an in-memory database dependency. For Maven projects, include the following:
- Advanced Configuration with Profiles: Utilize Spring Profiles to create test-specific configurations, ensuring no test accidentally affects production configurations.
- Testing Integration with Other Databases: Instead of an in-memory database, you might integrate an actual relational database for tests by configuring
@DataJpaTestwith@AutoConfigureTestDatabase. - Debugging Test Contexts: Utilize loggers for deeper insights into test context creation failures, focusing on
org.springframeworknamespaces to identify misconfigurations.
Related reading
- Spring boot 1.4 Testing Configuration error found multiple declarations of BootstrapWith
- Spring Boot 2.0 disable default security
- Spring boot 2.1 bean override vs. Primary
- Spring boot 2.4.0 The type HandlerInterceptorAdapter is deprecated
- Spring Boot 2.4.2 - DNS Resolution Problem at start on Apple M1
- Spring Boot 2.4.2 and Thymeleaf 3.0.12 - access static methods
- Spring Boot 2.5.0 generates plain.jar file. Can I remove it?
- Spring Boot 2.6.0 / Spring fox 3 - Failed to start bean 'documentationPluginsBootstrapper

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.