You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot项目中UserRepository无save(User)方法及User无getId()方法的错误解决求助

Fixing Your Spring Boot JPA Test Errors

Hey there! Let's tackle those two errors you're facing step by step:

Error 1: The method save(User) is undefined for the type UserRepository

This issue boils down to a mismatched test annotation in your UserRepositoryTests class. You’ve added both @DataJdbcTest and @DataJpaTest, but @DataJdbcTest is built for testing plain JDBC components—not JPA repositories like JpaRepository. When you use @DataJdbcTest, Spring skips initializing the Spring Data JPA infrastructure, so your UserRepository doesn’t get the proxy implementation that includes all built-in methods like save().

Fix:

Remove the @DataJdbcTest annotation and keep only @DataJpaTest (the correct annotation for testing JPA repositories). Your test class header should look like this:

@DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
@Rollback(false)
public class UserRepositoryTests {
    // rest of your code...
}

Error 2: The method getId() is undefined for the type User

Looking at your User entity class, you’ve defined the id field but haven’t added its getter method (you only included getters/setters for email, password, firstName, and lastName). When you call savedUser.getId() in your test, the compiler can’t find this method because it doesn’t exist.

Fix:

Add the getter and setter for the id field in your User class:

public class User {
    // existing fields...

    // Add these methods
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    // existing getters/setters...
}

Updated Code Snippets

Modified User Entity

package com.example.TEST_WEB;
import javax.persistence.*;
@Entity
@Table(name="users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(nullable = false, unique = true, length = 45)
    private String email;
    @Column(nullable = false, length = 64)
    private String password;
    @Column(name = "first_name", nullable = false, length = 20)
    private String firstName;
    @Column(name = "last_name", nullable = false, length = 20)
    private String lastName;

    // Added id getter/setter
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    // Existing getters/setters
    public String getEmail() { return this.email; }
    public void setEmail(String email) { this.email = email; }
    public String getPassword() { return this.password; }
    public void setPassword(String password) { this.password = password; }
    public String getFirstName() { return this.firstName; }
    public void setFirstName(String firstName) { this.firstName = firstName; }
    public String getLastName() { return this.lastName; }
    public void setLastName(String lastName) { this.lastName = lastName; }
}

Modified UserRepositoryTests

package com.example.TEST_WEB;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.annotation.Rollback;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

@DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
@Rollback(false)
public class UserRepositoryTests {
    @Autowired
    private UserRepository repo;
    @Autowired
    private TestEntityManager entityManager;

    @Test
    public void testCreatUser(){
        User user = new User();
        user.setEmail("ravikumar@gmail.com");
        user.setPassword("ravi2020");
        user.setFirstName("Ravi");
        user.setLastName("Kumar");
        User savedUser = repo.save(user);
        User existUser = entityManager.find(User.class, savedUser.getId());
        assertThat(user.getEmail()).isEqualTo(existUser.getEmail());
    }
}

After making these changes, your test should run without those two errors. Feel free to reach out if you hit any other snags!

内容的提问来源于stack exchange,提问作者work

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 11:07:51