BookServiceOldTest.java
1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package org.legrog.application;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.legrog.entities.Book;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaQuery;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(MockitoJUnitRunner.class)
public class BookServiceOldTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private EntityManager entityManager;
private BookServiceOld bookServiceOld;
@Before
public void setUp() throws Exception {
bookServiceOld = new BookServiceOld(entityManager);
}
@Test
public void testAddBook() throws Exception {
Book book = new Book();
bookServiceOld.addBook(book);
Mockito.verify(entityManager).persist(book);
}
@Test
public void testAllBooks() throws Exception {
Book book = new Book();
book.setBookId(0);
CriteriaQuery<Book> cq = Mockito.mock(CriteriaQuery.class);
Mockito.when(entityManager.getCriteriaBuilder().createQuery(Book.class))
.thenReturn(cq);
Mockito.when(entityManager.createQuery(cq).getResultList())
.thenReturn(Arrays.asList(book));
List<Book> allBooks = bookServiceOld.getAllBooks();
assertThat(allBooks).as("book list")
.hasSize(1)
.contains(book);
}
}