javaspring-boottesting

Spring Boot 4.0.0. cannot resolve symbol DataJpaTest


I am new to Java and Spring Boot. I am building a small CRUD app to learn about JPA and the H2 database.

This is my POM file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>4.0.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.robertforpresent</groupId>
    <artifactId>spring_api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring_api</name>
    <description>Spring API</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>25</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-h2console</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </path>
                    </annotationProcessorPaths>
                    <source>16</source>
                    <target>16</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <addResources>true</addResources>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <argLine>-XX:+EnableDynamicAgentLoading</argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

When I annotate a test with DataJPATest, it cannot resolve it. I tried with Claude Code, a new spring. Initializer pom (with JPA, test, and H2 dependencies) as well as searching the Web manually.

I found import statements like:

import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

However, the orm does not exist. I invalidated caches (I use IntelliJ), deleted .m2/org/springframework/boot, and did several mvn clean install. No solution yet.
I fear I am missing something very obvious, due to my little hands-on experience with either Java or Maven.

I found this: DataJpaTest and AutoConfigureTestDatabase in Spring Boot 4.0.0. But here "jpa.test" (test) is missing.


Solution

  • Spring Boot 4 modularizes the codebase so various things are moved to different modules/dependencies. This results in you needing more dependencies than with Spring Boot 3 (and sometimes different package names).

    If you take a look at the Module dependency starters table in the migration guide, you should see the following entry:

    Technology Main Dependency Test Dependency
    Spring Data JPA (using Hibernate) spring-boot-starter-data-jpa spring-boot-starter-data-jpa-test

    So, you need the spring-boot-starter-data-jpa-test dependency for testing code using spring-boot-starter-data-jpa:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa-test</artifactId>
        <scope>test</scope>
    </dependency>
    

    In fact, this dependency is also present in the question you linked which is why the answer works there.