|
1 | 1 | package com.hellokoding.jpa.book; |
2 | 2 |
|
3 | | -import lombok.NoArgsConstructor; |
4 | | -import org.junit.Assert; |
5 | 3 | import org.junit.Before; |
6 | 4 | import org.junit.Test; |
7 | 5 | import org.junit.runner.RunWith; |
8 | 6 | import org.springframework.beans.factory.annotation.Autowired; |
9 | 7 | import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; |
10 | | -import org.springframework.test.annotation.DirtiesContext; |
| 8 | +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; |
11 | 9 | import org.springframework.test.context.junit4.SpringRunner; |
12 | 10 |
|
13 | | -import java.util.Optional; |
14 | | - |
15 | | -@NoArgsConstructor |
| 11 | +import static org.assertj.core.api.Assertions.assertThat; |
16 | 12 |
|
17 | 13 | @RunWith(SpringRunner.class) |
18 | 14 | @DataJpaTest |
19 | | -@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) |
20 | 15 | public class DeletingDataTest { |
| 16 | + @Autowired |
| 17 | + private TestEntityManager testEntityManager; |
| 18 | + |
21 | 19 | @Autowired |
22 | 20 | private BookRepository bookRepository; |
23 | 21 |
|
24 | 22 | @Autowired |
25 | 23 | private CategoryRepository categoryRepository; |
26 | 24 |
|
| 25 | + private int givenCategoryId; |
| 26 | + |
27 | 27 | @Before |
28 | | - public void createTestData(){ |
29 | | - categoryRepository.save(new Category("A", new Book("A1"), new Book("A2"))); |
| 28 | + public void setUp(){ |
| 29 | + // given |
| 30 | + givenCategoryId = testEntityManager.persistAndFlush(new Category("A", new Book("A1"), new Book("A2"))).getId(); |
30 | 31 | } |
31 | 32 |
|
32 | 33 | @Test |
33 | | - public void testDelete_whenJPQL_thenSuccess() { |
34 | | - bookRepository.deleteByCategoryId(1); |
35 | | - Assert.assertEquals(0, bookRepository.findByCategoryId(1).size()); |
| 34 | + public void whenDeleteByJPQL_thenSuccess() { |
| 35 | + // when |
| 36 | + bookRepository.deleteByCategoryId(givenCategoryId); |
| 37 | + |
| 38 | + // then |
| 39 | + assertThat(bookRepository.findByCategoryId(givenCategoryId)).hasSize(0); |
36 | 40 | } |
37 | 41 |
|
38 | 42 | @Test |
39 | | - public void testDelete_whenOrphanRemoval_thenSuccess() { |
40 | | - Category category = categoryRepository.findById(1).get(); |
| 43 | + public void whenDeleteByOrphanRemoval_thenSuccess() { |
| 44 | + // when |
| 45 | + Category category = categoryRepository.findById(givenCategoryId).get(); |
41 | 46 | Book book = category.getBooks().iterator().next(); |
42 | 47 | int bookId = book.getId(); |
43 | 48 | category.getBooks().remove(book); |
44 | 49 | categoryRepository.flush(); |
45 | 50 |
|
46 | | - Assert.assertEquals(Optional.empty(), bookRepository.findById(bookId)); |
| 51 | + // then |
| 52 | + assertThat(bookRepository.findById(bookId)).isEmpty(); |
47 | 53 | } |
48 | 54 |
|
49 | 55 | @Test |
50 | | - public void testDelete_whenCascadeAll_thenSuccess() { |
51 | | - Category category = categoryRepository.findById(1).get(); |
| 56 | + public void whenDeleteByCascadeType_thenSuccess() { |
| 57 | + // when |
| 58 | + Category category = categoryRepository.findById(givenCategoryId).get(); |
52 | 59 | categoryRepository.delete(category); |
53 | 60 |
|
54 | | - Assert.assertEquals(0, bookRepository.findByCategoryId(1).size()); |
| 61 | + // then |
| 62 | + assertThat(bookRepository.findByCategoryId(givenCategoryId)).hasSize(0); |
55 | 63 | } |
56 | 64 | } |
0 commit comments