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 pathBookResourceTest.java
More file actions
133 lines (113 loc) · 4.74 KB
/
Copy pathBookResourceTest.java
File metadata and controls
133 lines (113 loc) · 4.74 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package nl.first8.generativetesting.rest;
import org.hamcrest.beans.SamePropertyValuesAs;
import org.junit.runner.RunWith;
import com.pholser.junit.quickcheck.From;
import com.pholser.junit.quickcheck.Property;
import com.pholser.junit.quickcheck.generator.Ctor;
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.generator.java.lang.StringGenerator;
import com.pholser.junit.quickcheck.generator.java.time.LocalDateGenerator;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck;
import nl.first8.generativetesting.Author;
import nl.first8.generativetesting.Book;
import nl.first8.generativetesting.Publisher;
import nl.first8.generativetesting.rest.BookResource;
import static org.junit.Assert.assertThat;
import java.time.LocalDate;
@RunWith(JUnitQuickcheck.class)
public final class BookResourceTest {
/**
* Example using build types, which junit-quickcheck can generate.
*
* @param title generated by junit-quickcheck
* @param author generated by junit-quickcheck
* @param publisher generated by junit-quickcheck
* @param publishedOn generated by junit-quickcheck
*/
@Property
public void testConversions(//
final String title, //
final String author, //
final String publisher, //
final LocalDate publishedOn) {
// Given a book
final Book book = new Book(title, new Author(author),
new Publisher(publisher), publishedOn);
// When we create a resource based on that book
final BookResource resource = BookResource.fromBook(book);
// And convert the resource back to a book
final Book converted = resource.toBook();
// Then we expected the result to have the same property values as the
// original book
assertThat(converted, SamePropertyValuesAs.samePropertyValuesAs(book));
}
/**
* Example using a user defined type, of which all constructor parameters are
* build-in types.
*
* @param resource generated by junit-quickcheck
*/
@Property
public void testConversions(// Given a resource
final @From(Ctor.class) BookResource resource) {
// When we convert the resource into a book
final Book book = resource.toBook();
// And we convert the book back to a resource
final BookResource converted = BookResource.fromBook(book);
// Then we expect the result to have the same property values as
// the original resource
assertThat(converted,
SamePropertyValuesAs.samePropertyValuesAs(resource));
}
/**
* Because book has a constructor with user defined data types (Author and
* Publisher), the Ctor.class generator will not work.
*
* Therefore, we need to create a generator, to generate a book.
*/
public static final class BookGenerator extends Generator<Book> {
/**
* Generator to generate strings for title, author name and publisher
* name.
*/
private final StringGenerator stringGenerator = new StringGenerator();
/**
* Generator to generate dates.
*/
private final LocalDateGenerator localDateGenerator =
new LocalDateGenerator();
public BookGenerator() {
super(Book.class);
}
@Override
public Book generate(final SourceOfRandomness random,
final GenerationStatus status) {
final String title = stringGenerator.generate(random, status);
final String author = stringGenerator.generate(random, status);
final String publisher = stringGenerator.generate(random, status);
final LocalDate publishedOn =
localDateGenerator.generate(random, status);
final Book book = new Book(title, new Author(author),
new Publisher(publisher), publishedOn);
return book;
}
}
/**
* Example using the BookGenerator defined above, to generate books
*
* @param book generated by junit-quickcheck
*/
@Property
public void testConversions(// Given a book
final @From(BookGenerator.class) Book book) {
// When we convert the book to a resource
final BookResource resource = BookResource.fromBook(book);
// and we convert the resource back to a book
final Book converted = resource.toBook();
// Then we expect the result to have the same property values as the
// original book
assertThat(converted, SamePropertyValuesAs.samePropertyValuesAs(book));
}
}