๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:863)โก Quick Fix Works 80% of the time
Ensure spring-boot-starter-data-jpa and an H2 database dependency are in your pom.xml for tests.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>๐ง Why this Happens
Tap to expand the deep technical explanation
You used `@DataJpaTest` or attempted to inject `EntityManagerFactory`, but Spring Boot's auto-configuration didn't create it. This happens if the JPA starter is missing, the database driver is missing, or you excluded JPA auto-configuration in your test properties.
The HITEC City Parking Spot Analogy:
It's like a factory inspector arriving to check the assembly line (EntityManagerFactory), but the factory floor is empty because the machinery (JPA starter) was never delivered. The inspector can't inspect what isn't there.
๐ How to Reproduce Confirm this is your error
Create a test annotated with `@DataJpaTest`. Remove `spring-boot-starter-data-jpa` from your `pom.xml`. Run the test.
๐ ๏ธ Solutions (5 Ways to Fix)
Add JPA and H2 dependencies
๐ Use this when setting up a new JPA project.
`@DataJpaTest` requires the JPA starter to build the factory, and an embedded database (H2) to run it.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>Remove excludeFilters from @DataJpaTest
๐ Use this if you accidentally told @DataJpaTest to skip JPA configuration.
Ensure you haven't written `@DataJpaTest(excludeAutoConfiguration = ...)` which strips out the EntityManagerFactory.
// Just use standard annotation:
@DataJpaTest
public class UserRepositoryTest { ... }Fix multiple DataSource configuration
๐ Use this if you have multiple databases configured.
If Spring finds two DataSources, it doesn't know which one to use to build the EntityManagerFactory. Mark one as @Primary.
@Bean
@Primary
public DataSource primaryDataSource() { ... }Use @AutoConfigureTestDatabase
๐ Use this if Spring is trying to use your real MySQL database instead of H2 for tests.
This annotation forces Spring to replace your configured database with an in-memory one for the test.
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY)
public class UserRepositoryTest { ... }Check test properties file
๐ Use this if you have an `application-test.properties` file.
Ensure you haven't set `spring.autoconfigure.exclude=DataSourceAutoConfiguration` in your test properties.
# src/test/resources/application-test.properties
# Remove this line:
# spring.autoconfigure.exclude=DataSourceAutoConfiguration๐ Version Notes
Uses H2 1.4 for tests.
Uses H2 2.x. Requires Java 17. `@DataJpaTest` is stricter.
๐ก๏ธ How to Prevent This Next Time
Use Spring Initializr to generate your project with the 'Spring Data JPA' and 'H2 Database' dependencies pre-selected for the test scope.