Search code examples
javaspring-bootmavenjpa

The method save(User) is undefined for the type UserRepositoryJava(67108964)


I'm new to Java and Spring Boot. I created a Spring Boot project. and I created an entity class with this code:

package com.example.TEST_WEB;

import javax.persistence.*;

@Entity
@Table(name="users")
public class User {
    @Id
    //Strategy = GenerationType. IDENTITY : La génération de la clé primaire se fera à partir d’une Identité propre au SGBD.
    // Il utilise un type de colonne spéciale à la base de données.
    @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;
     
    // getters and setters are not shown 

    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;
    }

}

I also created an interface with this code:

import com.example.TEST_WEB.User;
import org.springframework.data.jpa.repository.JpaRepository;
 
public interface UserRepository extends JpaRepository<User, Long> {
 
}

And to test my code, I created this class:

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.data.jdbc.DataJdbcTest;
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;

@DataJdbcTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
//une transaction gérée par un test doit être annulée une fois la méthode de test terminée
@Rollback(false)

public class UserRepositoryTests {
 
    @Autowired
    private UserRepository repo;

    @Autowired
    private TestEntityManager entityManager;
     
   
    // test methods go below
    @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());
     // assert c'est pour la vérification
    assertThat(user.getEmail()).isEqualTo(existUser.getEmail());

    }
}

This gives me two errors:

  • The method save(User) is undefined for the type UserRepositoryJava(67108964)

  • The method getId() is undefined for the type UserJava(67108964)


Solution

    1. You must move the UserRepository to the package com.example.TEST_WEB

    2. Then you must remove UserRepository from the src/test/java

    3. And finally add getId() and setId() methods to the User class