Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47171ef

Browse files
committed
Some work on testing
1 parent ae28a22 commit 47171ef

File tree

4 files changed

+55
-41
lines changed

4 files changed

+55
-41
lines changed

src/test/groovy/nl/topicus/overheid/javafactorybot/AbstractModelFactoryTest.groovy

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package nl.topicus.overheid.javafactorybot
2+
3+
import nl.topicus.overheid.javafactorybot.test.factory.ArticleFactory
4+
import nl.topicus.overheid.javafactorybot.test.model.Article
5+
import nl.topicus.overheid.javafactorybot.test.model.User
6+
import spock.lang.Specification
7+
import spock.lang.Subject
8+
9+
class BaseFactoryTest extends Specification {
10+
@Subject
11+
ArticleFactory factory = new ArticleFactory()
12+
13+
def "it fills the attributes when no overrides are given"() {
14+
when:
15+
Article article = new ArticleFactory().build()
16+
17+
then: "defined attributes with default are filled"
18+
article.title != null
19+
article.content != null
20+
article.creationDate != null
21+
22+
and: "defined attributes with default null are null"
23+
article.summary == null
24+
25+
and: "relations are set"
26+
article.author != null
27+
article.author instanceof User
28+
article.comments != null
29+
article.comments.size() == 0
30+
31+
and: "nondefined attributes are not filled"
32+
article.slug == null
33+
}
34+
35+
def "it uses overrides"() {
36+
when:
37+
Article article = factory.build([title: "foo", slug: "derp"])
38+
39+
then: "overrides are used"
40+
article.title == "foo"
41+
article.slug == "derp"
42+
43+
and: "remaining attributes are filled as default"
44+
article.content != null
45+
article.creationDate != null
46+
article.summary == null
47+
article.author != null
48+
article.author instanceof User
49+
article.comments != null
50+
article.comments.size() == 0
51+
}
52+
}

src/test/groovy/nl/topicus/overheid/javafactorybot/test/factory/ArticleFactory.groovy

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class ArticleFactory extends Factory<Article> {
1414
title : attribute { faker.lorem().sentence() },
1515
content : attribute { faker.lorem().paragraph() },
1616
creationDate: attribute { faker.date().past(20, TimeUnit.DAYS) },
17+
summary : attribute { null },
1718
author : hasOne(UserFactory),
1819
comments : hasMany(CommentFactory)
1920
]
@@ -24,11 +25,6 @@ class ArticleFactory extends Factory<Article> {
2425
article?.comments.each { it.article = article }
2526
}
2627

27-
@Override
28-
void onAfterAttributes(Map<String, Object> attributes) {
29-
attributes.put("title", "foo")
30-
}
31-
3228
@Override
3329
Map<String, Trait> getTraits() {
3430
[

src/test/java/nl/topicus/overheid/javafactorybot/test/model/Article.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class Article {
1111
private String title;
1212
private String content;
1313
private Date creationDate;
14+
private String slug;
15+
private String summary;
1416

1517
// Relations
1618
private User author;

0 commit comments

Comments
 (0)