🔴 The Error You're Seeing

Confirm this matches your console output. If it does, you're in the right place.

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

Solution 1✓ Most common cause

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>
Solution 2

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 { ... }
Solution 3

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

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 { ... }
Solution 5

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

Spring Boot 2.x

Uses H2 1.4 for tests.

Spring Boot 3.x

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.