This repository was archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookControllerTest.java
More file actions
109 lines (91 loc) · 3.59 KB
/
Copy pathBookControllerTest.java
File metadata and controls
109 lines (91 loc) · 3.59 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package nl.first8.generativetesting.rest;
import org.apache.commons.lang3.tuple.Pair;
import org.hamcrest.beans.SamePropertyValuesAs;
import org.junit.Rule;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import com.google.common.collect.Streams;
import com.pholser.junit.quickcheck.From;
import com.pholser.junit.quickcheck.Property;
import com.pholser.junit.quickcheck.generator.Ctor;
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck;
import nl.first8.generativetesting.Book;
import nl.first8.generativetesting.BookService;
import nl.first8.generativetesting.rest.BookController;
import nl.first8.generativetesting.rest.BookResource;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.stream.Collectors;
@RunWith(JUnitQuickcheck.class)
public final class BookControllerTest {
/*
* The usual Mockito mocks and rules
*/
@Mock
private BookService bookService;
@InjectMocks
private BookController bookController;
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
/*
* Property based testing using mocks
*/
/**
* Tests the book controller against a mock book service, using
* junit-quickcheck to generate test data.
*
* @param resources generated by junit-quickcheck
*/
@Property()
public void testBasedOnResources(//
final List<@From(Ctor.class) BookResource> resources) {
// Given an mocked bookservice, which returns generated data when
// findAll() is called.
when(bookService.findAll()).thenReturn(resources.stream()
.map(BookResource::toBook).collect(Collectors.toList()));
// When we call books() on the book controller
final List<BookResource> results = bookController.books();
// We expect the book controller to return book resources which match
// the books
Streams //
.zip(resources.stream(), results.stream(), Pair::of)//
.forEach(pair -> {
final BookResource reference = pair.getLeft();
final BookResource result = pair.getRight();
assertThat( //
reference, //
SamePropertyValuesAs.samePropertyValuesAs(result));
});
}
/**
* Tests the book controller against a mock book service, using
* junit-quickcheck to generate test data.
*
* @param books generated by junit-quickcheck
*/
@Property()
public void testBasedOnBooks(//
final List<
@From(BookResourceTest.BookGenerator.class) Book> books) {
// Given an mocked bookservice, which returns generated data when
// findAll() is called.
when(bookService.findAll()).thenReturn(books);
// When we call books() on the book controller
final List<BookResource> resources = bookController.books();
// We expect the book controller to return book resources which match
// the books
Streams //
.zip(resources.stream(), books.stream(), Pair::of)//
.forEach(pair -> {
final BookResource resource = pair.getLeft();
final Book book = pair.getRight();
assertThat( //
resource.toBook(), //
SamePropertyValuesAs.samePropertyValuesAs(book));
});
}
}