Skip to content

Commit a2ed433

Browse files
authored
Merge pull request eugenp#6455 from kacperkoza/master
Spy, mock and stub in Spock Framework
2 parents b8225bf + 84c8c70 commit a2ed433

8 files changed

Lines changed: 237 additions & 2 deletions

File tree

‎testing-modules/groovy-spock/pom.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848
</build>
4949

5050
<properties>
51-
<spock-core.version>1.0-groovy-2.4</spock-core.version>
51+
<spock-core.version>1.3-RC1-groovy-2.4</spock-core.version>
5252
<groovy-all.version>2.4.7</groovy-all.version>
5353
<gmavenplus-plugin.version>1.5</gmavenplus-plugin.version>
5454
</properties>
5555

56-
</project>
56+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package mocks;
2+
3+
public interface EventPublisher {
4+
5+
void publish(String addedOfferId);
6+
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package mocks;
2+
3+
public class ExternalItemProviderException extends RuntimeException {
4+
5+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package mocks;
2+
3+
import java.util.Objects;
4+
5+
public class Item {
6+
private final String id;
7+
private final String name;
8+
9+
public Item(String id, String name) {
10+
this.id = id;
11+
this.name = name;
12+
}
13+
14+
public String getId() {
15+
return id;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
@Override
23+
public boolean equals(Object o) {
24+
if (this == o) return true;
25+
if (o == null || getClass() != o.getClass()) return false;
26+
Item item = (Item) o;
27+
return Objects.equals(id, item.id) &&
28+
Objects.equals(name, item.name);
29+
}
30+
31+
@Override
32+
public int hashCode() {
33+
return Objects.hash(id, name);
34+
}
35+
36+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package mocks;
2+
3+
import java.util.List;
4+
5+
public interface ItemProvider {
6+
7+
List<Item> getItems(List<String> itemIds);
8+
9+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package mocks;
2+
3+
import java.util.Comparator;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
public class ItemService {
8+
private final ItemProvider itemProvider;
9+
private final EventPublisher eventPublisher;
10+
11+
12+
public ItemService(ItemProvider itemProvider, EventPublisher eventPublisher) {
13+
this.itemProvider = itemProvider;
14+
this.eventPublisher = eventPublisher;
15+
}
16+
17+
List<Item> getAllItemsSortedByName(List<String> itemIds) {
18+
List<Item> items;
19+
try{
20+
items = itemProvider.getItems(itemIds);
21+
} catch (RuntimeException ex) {
22+
throw new ExternalItemProviderException();
23+
}
24+
return items.stream()
25+
.sorted(Comparator.comparing(Item::getName))
26+
.collect(Collectors.toList());
27+
}
28+
29+
void saveItems(List<String> itemIds) {
30+
List<String> notEmptyOfferIds = itemIds.stream()
31+
.filter(itemId -> !itemId.isEmpty())
32+
.collect(Collectors.toList());
33+
// save in database
34+
notEmptyOfferIds.forEach(eventPublisher::publish);
35+
}
36+
37+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package mocks;
2+
3+
public class LoggingEventPublisher implements EventPublisher {
4+
5+
@Override
6+
public void publish(String addedOfferId) {
7+
System.out.println("I've published: " + addedOfferId);
8+
}
9+
10+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package mocks
2+
3+
import spock.lang.Specification
4+
5+
class ItemServiceUnitTest extends Specification {
6+
7+
ItemProvider itemProvider
8+
ItemService itemService
9+
EventPublisher eventPublisher
10+
11+
def setup() {
12+
itemProvider = Stub(ItemProvider)
13+
eventPublisher = Mock(EventPublisher)
14+
itemService = new ItemService(itemProvider, eventPublisher)
15+
}
16+
17+
def 'should return items sorted by name'() {
18+
given:
19+
def ids = ['offer-id', 'offer-id-2']
20+
itemProvider.getItems(ids) >> [new Item('offer-id-2', 'Zname'), new Item('offer-id', 'Aname')]
21+
22+
when:
23+
List<Item> items = itemService.getAllItemsSortedByName(ids)
24+
25+
then:
26+
items.collect { it.name } == ['Aname', 'Zname']
27+
}
28+
29+
def 'arguments constraints'() {
30+
itemProvider.getItems(['offer-id'])
31+
itemProvider.getItems(_) >> []
32+
itemProvider.getItems(*_) >> []
33+
itemProvider.getItems(!null) >> []
34+
itemProvider.getItems({ it.size > 0 }) >> []
35+
}
36+
37+
def 'should return different items on subsequent call'() {
38+
given:
39+
itemProvider.getItems(_) >>> [
40+
[],
41+
[new Item('1', 'name')],
42+
[new Item('2', 'name')]
43+
]
44+
45+
when: 'method is called for the first time'
46+
List<Item> items = itemService.getAllItemsSortedByName(['not-important'])
47+
48+
then: 'empty list is returned'
49+
items == []
50+
51+
when: 'method is called for the second time'
52+
items = itemService.getAllItemsSortedByName(['not-important'])
53+
54+
then: 'item with id=1 is returned'
55+
items == [new Item('1', 'name')]
56+
57+
when: 'method is called for the thirdtime'
58+
items = itemService.getAllItemsSortedByName(['not-important'])
59+
60+
then: 'item with id=2 is returned'
61+
items == [new Item('2', 'name')]
62+
}
63+
64+
def 'should throw ExternalItemProviderException when ItemProvider fails'() {
65+
given:
66+
itemProvider.getItems(_) >> { new RuntimeException()}
67+
68+
when:
69+
itemService.getAllItemsSortedByName([])
70+
71+
then:
72+
thrown(ExternalItemProviderException)
73+
}
74+
75+
def 'chaining response'() {
76+
itemProvider.getItems(_) >>> { new RuntimeException() } >> new SocketTimeoutException() >> [new Item('id', 'name')]
77+
}
78+
79+
def 'should return different items for different ids lists'() {
80+
given:
81+
def firstIds = ['first']
82+
def secondIds = ['second']
83+
itemProvider.getItems(firstIds) >> [new Item('first', 'Zname')]
84+
itemProvider.getItems(secondIds) >> [new Item('second', 'Aname')]
85+
86+
when:
87+
def firstItems = itemService.getAllItemsSortedByName(firstIds)
88+
def secondItems = itemService.getAllItemsSortedByName(secondIds)
89+
90+
then:
91+
firstItems.first().name == 'Zname'
92+
secondItems.first().name == 'Aname'
93+
}
94+
95+
def 'should publish events about new non-empty saved offers'() {
96+
given:
97+
def offerIds = ['', 'a', 'b']
98+
99+
when:
100+
itemService.saveItems(offerIds)
101+
102+
then:
103+
2 * eventPublisher.publish({ it != null && !it.isEmpty()})
104+
}
105+
106+
def 'should return items'() {
107+
given:
108+
itemProvider = Mock(ItemProvider)
109+
itemProvider.getItems(['item-id']) >> [new Item('item-id', 'name')]
110+
itemService = new ItemService(itemProvider, eventPublisher)
111+
112+
when:
113+
def items = itemService.getAllItemsSortedByName(['item-id'])
114+
115+
then:
116+
items == [new Item('item-id', 'name')]
117+
}
118+
119+
def 'should spy on EventPublisher method call'() {
120+
given:
121+
LoggingEventPublisher eventPublisher = Spy(LoggingEventPublisher)
122+
itemService = new ItemService(itemProvider, eventPublisher)
123+
124+
when:
125+
itemService.saveItems(['item-id'])
126+
127+
then:
128+
1 * eventPublisher.publish('item-id')
129+
}
130+
131+
}

0 commit comments

Comments
 (0)