-
Notifications
You must be signed in to change notification settings - Fork 151
[4기 박현지] Mission 2: 영속성 컨텍스트 #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: hyeon-z
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.prgms.springbootjpa.mission2.domain.customer; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
@Table(name = "customers") | ||
public class Customer { | ||
@Id | ||
Long id; | ||
String firstName; | ||
String lastName; | ||
|
||
public void changeFirstName(String firstName) { | ||
this.firstName = firstName; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package org.prgms.springbootjpa.mission2.domain.customer; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.prgms.springbootjpa.mission1.customer.repository.CustomerRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.EntityTransaction; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
|
||
@SpringBootTest | ||
class CustomerRepositoryTest { | ||
@Autowired | ||
EntityManagerFactory emf; | ||
|
||
@Autowired | ||
CustomerRepository repository; | ||
|
||
EntityManager entityManager; | ||
|
||
EntityTransaction transaction; | ||
|
||
|
||
@BeforeEach | ||
void setUp() { | ||
entityManager = emf.createEntityManager(); | ||
transaction = entityManager.getTransaction(); | ||
repository.deleteAll(); | ||
} | ||
|
||
Customer customer = new Customer(1L, "hyeonji", "park"); | ||
|
||
@Test | ||
void 저장() { | ||
transaction.begin(); | ||
|
||
entityManager.persist(customer); | ||
|
||
transaction.commit(); | ||
} | ||
|
||
@Test | ||
void 조회_1차캐시_이용() { | ||
transaction.begin(); | ||
|
||
entityManager.persist(customer); | ||
|
||
transaction.commit(); | ||
|
||
Customer entity = entityManager.find(Customer.class, customer.getId()); | ||
assertThat(entity, samePropertyValuesAs(customer)); | ||
} | ||
|
||
|
||
@Test | ||
void 조회_DB_이용() { | ||
transaction.begin(); | ||
|
||
entityManager.persist(customer); | ||
|
||
transaction.commit(); | ||
|
||
entityManager.detach(customer); | ||
|
||
Customer entity = entityManager.find(Customer.class, customer.getId()); | ||
assertThat(entity, samePropertyValuesAs(customer)); | ||
} | ||
|
||
@Test | ||
void 수정_dirtyCheck_적용() { | ||
transaction.begin(); | ||
|
||
entityManager.persist(customer); | ||
|
||
transaction.commit(); | ||
|
||
transaction.begin(); | ||
|
||
customer.changeFirstName("hyeonz"); | ||
|
||
transaction.commit(); | ||
|
||
Customer entity = entityManager.find(Customer.class, customer.getId()); | ||
assertThat(entity.getFirstName(), is("hyeonz")); | ||
} | ||
|
||
@Test | ||
void 수정_dirtyCheck_적용_안됨() { | ||
transaction.begin(); | ||
|
||
entityManager.persist(customer); | ||
|
||
transaction.commit(); | ||
|
||
entityManager.detach(customer); | ||
|
||
transaction.begin(); | ||
|
||
customer.changeFirstName("hyeonz"); | ||
|
||
transaction.commit(); | ||
|
||
Customer afterEntity = entityManager.find(Customer.class, customer.getId()); | ||
assertThat(afterEntity.getFirstName(), not("hyeonz")); | ||
} | ||
|
||
@Test | ||
void 삭제() { | ||
transaction.begin(); | ||
|
||
entityManager.persist(customer); | ||
|
||
transaction.commit(); | ||
|
||
transaction.begin(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 삭제에 대한 검증이니 transaction.begin()은 remove() 전에 선언하는것은 어떨까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 첫 번째 |
||
|
||
Customer beforeCommit = entityManager.find(Customer.class, customer.getId()); | ||
assertThat(beforeCommit, samePropertyValuesAs(customer)); | ||
|
||
entityManager.remove(customer); | ||
|
||
transaction.commit(); | ||
|
||
Customer afterCommit = entityManager.find(Customer.class, customer.getId()); | ||
assertThat(afterCommit, nullValue()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
반복되는 EntityManager 생성을 setup method를 통해서 하는건 어떨까요?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵! 적용했습니다~!
refactor: 공통 메서드 https://github.com/beforeeach 메서드로 분리