|
| 1 | +package com.sehee.weeklyjpa; |
| 2 | + |
| 3 | +import com.sehee.weeklyjpa.domain.Customer; |
| 4 | +import com.sehee.weeklyjpa.domain.CustomerRepository; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.junit.jupiter.api.AfterEach; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.DisplayName; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.context.SpringBootTest; |
| 12 | +import org.springframework.transaction.annotation.Transactional; |
| 13 | + |
| 14 | +import java.util.Optional; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | + |
| 18 | +@Slf4j |
| 19 | +@SpringBootTest |
| 20 | +public class JpaTest { |
| 21 | + @Autowired |
| 22 | + CustomerRepository repository; |
| 23 | + |
| 24 | + Customer customer; |
| 25 | + |
| 26 | + @BeforeEach |
| 27 | + void setUp() { |
| 28 | + customer = new Customer(); |
| 29 | + customer.setId(1L); |
| 30 | + customer.setFirstName("Sehee"); |
| 31 | + customer.setLastName("Lee"); |
| 32 | + } |
| 33 | + |
| 34 | + @AfterEach |
| 35 | + void tearDown() { |
| 36 | + repository.deleteAll(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + @DisplayName("고객을 저장하고 조회할 수 있다.") |
| 41 | + void insertTest() { |
| 42 | + //When |
| 43 | + repository.save(customer); |
| 44 | + |
| 45 | + //Then |
| 46 | + Customer retrievedCustomer = repository.findById(customer.getId()).get(); |
| 47 | + log.info("{} {}", retrievedCustomer.getFirstName(), retrievedCustomer.getLastName()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + @Transactional |
| 52 | + @DisplayName("고객을 업데이트할 수 있다.") |
| 53 | + void updateTest() { |
| 54 | + //Given |
| 55 | + repository.save(customer); |
| 56 | + |
| 57 | + //When |
| 58 | + Customer retrievedCustomer = repository.findById(customer.getId()).get(); |
| 59 | + retrievedCustomer.setLastName("Kim"); |
| 60 | + |
| 61 | + //Then |
| 62 | + Customer updatedCustomer = repository.findById(customer.getId()).get(); |
| 63 | + log.info("{} {}", updatedCustomer.getFirstName(), updatedCustomer.getLastName()); |
| 64 | + } |
| 65 | + @Test |
| 66 | + @Transactional |
| 67 | + @DisplayName("고객을 삭제할 수 있다.") |
| 68 | + void deleteTest() { |
| 69 | + //Given |
| 70 | + repository.save(customer); |
| 71 | + |
| 72 | + //When |
| 73 | + repository.delete(customer); |
| 74 | + |
| 75 | + Optional<Customer> customerOptional = repository.findById(customer.getId()); |
| 76 | + assertThat(customerOptional).isEmpty(); |
| 77 | + } |
| 78 | +} |
0 commit comments