Here's a simple test. When I write @DataJpaTest, assertSame is true, but in @SpringBootTest, assertSame is false. Why is that? It's an object with the same ID, so I think the exact same object should come out. Please let me know.
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Data;
@Entity
@Data
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long eno;
}
import org.springframework.data.jpa.repository.JpaRepository;
import com.doding.board.domain.Employee;
public interface EmployeeRepository extends JpaRepository<Employee, Long> {}
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.doding.board.domain.Employee;
import com.doding.board.repo.EmployeeRepository;
@SpringBootTest
//@DataJpaTest
public class IdentityTest {
@Autowired
EmployeeRepository repo;
@Test
void identityTest() {
Employee emp = new Employee();
repo.save(emp);
Employee semp = repo.findById(emp.getEno()).get();
assertEquals(emp, semp);
assertSame(emp, semp);
}
}
When @SpringBootTest and @Transactional are used together, the result of assertSame is true.