lundi 27 avril 2020

Spring Boot: H2 Not Actually Retrieving From Database

I'm trying to write a simple Repository test in Spring Boot. The Test code looks like this:

public class UserRepositoryTest {
    private final TestEntityManager entityManager;
    private final UserRepository userRepository;

    @Autowired
    public UserRepositoryTest(TestEntityManager entityManager, UserRepository userRepository) {
        this.entityManager = entityManager;
        this.userRepository = userRepository;
    }

    @Test
    public void test() {
        String firstName = "Frank";
        String lastName = "Sample";
        String email = "frank@example.com";
        String username = "frank@example.com";
        String password = "floople";
        String passwordConfirm = "floople";

        RegisterUserRequest registerUserRequest = new RegisterUserRequest(firstName, lastName, email, username, password, passwordConfirm);
        User user = new User(registerUserRequest);
        user.setSpinsRemaining(0);
        userRepository.save(user);
        userRepository.setSpinsRemainingToTen();
        User found = userRepository.findByUsername(username);
        assertThat(found.getSpinsRemaining()).isEqualTo(10);
    }

What's I expect to happen is that the new User object is persisted to the database, the row in the database is modified to set spinsRemaining to 10, and then the now-modified row is retrieved from H2 and shoved into a new variable named "found". The "found" variable will point to an instance of a User object with ten spins remaining.

What actually happens is that the "found" variable points to the exact same instance of User that the "user" variable is. In fact, if I modify some property of the "user" variable AFTER persisting it to H2, the resultant "found" object also has the modified property. According to IntelliJ, both "user" and "found" are pointing to the same thing. How is that possible?

Aucun commentaire:

Enregistrer un commentaire