Skip to content

Commit 5f17da2

Browse files
committed
JPA Delete By
1 parent 74c9101 commit 5f17da2

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

jpa-hibernate-examples/jpa-hibernate-crud-deleting-entity/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<parent>
1212
<groupId>org.springframework.boot</groupId>
1313
<artifactId>spring-boot-starter-parent</artifactId>
14-
<version>2.1.1.RELEASE</version>
14+
<version>2.1.4.RELEASE</version>
1515
</parent>
1616

1717
<properties>
Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,64 @@
11
package com.hellokoding.jpa.book;
22

3-
import lombok.NoArgsConstructor;
4-
import org.junit.Assert;
53
import org.junit.Before;
64
import org.junit.Test;
75
import org.junit.runner.RunWith;
86
import org.springframework.beans.factory.annotation.Autowired;
97
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;
119
import org.springframework.test.context.junit4.SpringRunner;
1210

13-
import java.util.Optional;
14-
15-
@NoArgsConstructor
11+
import static org.assertj.core.api.Assertions.assertThat;
1612

1713
@RunWith(SpringRunner.class)
1814
@DataJpaTest
19-
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
2015
public class DeletingDataTest {
16+
@Autowired
17+
private TestEntityManager testEntityManager;
18+
2119
@Autowired
2220
private BookRepository bookRepository;
2321

2422
@Autowired
2523
private CategoryRepository categoryRepository;
2624

25+
private int givenCategoryId;
26+
2727
@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();
3031
}
3132

3233
@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);
3640
}
3741

3842
@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();
4146
Book book = category.getBooks().iterator().next();
4247
int bookId = book.getId();
4348
category.getBooks().remove(book);
4449
categoryRepository.flush();
4550

46-
Assert.assertEquals(Optional.empty(), bookRepository.findById(bookId));
51+
// then
52+
assertThat(bookRepository.findById(bookId)).isEmpty();
4753
}
4854

4955
@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();
5259
categoryRepository.delete(category);
5360

54-
Assert.assertEquals(0, bookRepository.findByCategoryId(1).size());
61+
// then
62+
assertThat(bookRepository.findByCategoryId(givenCategoryId)).hasSize(0);
5563
}
5664
}

0 commit comments

Comments
 (0)