[BAEL-16669] moved testing-modules/testing modules into relevant folders

This commit is contained in:
Sjmillington
2019-08-31 11:38:35 +01:00
parent bfba59a42f
commit ff871516ee
594 changed files with 1591 additions and 1951 deletions

View File

@@ -1 +0,0 @@
/report-*.json

View File

@@ -1,5 +0,0 @@
### Relevant articles
- [Introduction to Testing with Spock and Groovy](http://www.baeldung.com/groovy-spock)
- [Difference Between Stub, Mock, and Spy in the Spock Framework](https://www.baeldung.com/spock-stub-mock-spy)
- [Guide to Spock Extensions](https://www.baeldung.com/spock-extensions)

View File

@@ -1,56 +0,0 @@
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.spockframework</groupId>
<artifactId>groovy-spock</artifactId>
<version>1.0-SNAPSHOT</version>
<name>groovy-spock</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>${spock-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy-all.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>${gmavenplus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<spock-core.version>1.3-groovy-2.4</spock-core.version>
<groovy-all.version>2.4.7</groovy-all.version>
<gmavenplus-plugin.version>1.5</gmavenplus-plugin.version>
</properties>
</project>

View File

@@ -1,402 +0,0 @@
loadLogFile([{
"package": "mocks",
"name": "ExampleSpockTest",
"start": 1553898111660,
"features": [
{
"name": "should calculate character occurrences in given string",
"start": 1553898111662,
"end": 1553898111699,
"result": "passed",
"attachments": [
]
}
],
"end": 1553898111709,
"result": "passed",
"attachments": [
]
}])
loadLogFile([{
"package": "mocks",
"name": "ItemServiceTest",
"start": 1553898111714,
"features": [
{
"name": "should spy on EventPublisher method call",
"start": 1553898111714,
"output": [
"I've published: item-id\n"
],
"end": 1553898112250,
"result": "passed",
"attachments": [
]
},
{
"name": "should return items",
"start": 1553898112250,
"end": 1553898112260,
"result": "passed",
"attachments": [
]
},
{
"name": "should publish events about new non-empty saved offers",
"start": 1553898112260,
"end": 1553898112267,
"result": "passed",
"attachments": [
]
},
{
"name": "should return different items for different ids lists",
"start": 1553898112267,
"end": 1553898112280,
"result": "passed",
"attachments": [
]
},
{
"name": "should throw ExternalItemProviderException when ItemProvider fails",
"start": 1553898112281,
"end": 1553898112294,
"result": "passed",
"attachments": [
]
},
{
"name": "should return different items on subsequent call",
"start": 1553898112294,
"narrative": "When method is called for the first time\nThen empty list is returned\nWhen method is called for the second time\nThen item with id=1 is returned\nWhen method is called for the thirdtime\nThen item with id=2 is returned",
"end": 1553898112298,
"result": "passed",
"attachments": [
]
},
{
"name": "should return items sorted by name",
"start": 1553898112299,
"end": 1553898112307,
"result": "passed",
"attachments": [
]
}
],
"end": 1553898112310,
"result": "passed",
"attachments": [
]
}])
loadLogFile([{
"package": "FirstSpecification",
"name": "FirstSpecification",
"start": 1553898112314,
"features": [
{
"name": "Should verify notify was called",
"start": 1553898112314,
"end": 1553898112324,
"result": "passed",
"attachments": [
]
},
{
"name": "Should return true value for mock",
"start": 1553898112325,
"end": 1553898112344,
"result": "passed",
"attachments": [
]
},
{
"name": "Should return default value for mock",
"start": 1553898112344,
"end": 1553898112347,
"result": "passed",
"attachments": [
]
},
{
"name": "numbers to the power of two",
"start": 1553898112347,
"end": 1553898112358,
"result": "passed",
"attachments": [
]
},
{
"name": "Should get an index out of bounds when removing a non-existent item",
"start": 1553898112358,
"end": 1553898112364,
"result": "passed",
"attachments": [
]
},
{
"name": "Should be able to remove from list",
"start": 1553898112364,
"end": 1553898112366,
"result": "passed",
"attachments": [
]
},
{
"name": "two plus two should equal four",
"start": 1553898112366,
"end": 1553898112368,
"result": "passed",
"attachments": [
]
},
{
"name": "one plus one should equal two",
"start": 1553898112368,
"end": 1553898112391,
"result": "passed",
"attachments": [
]
}
],
"end": 1553898112394,
"result": "passed",
"attachments": [
]
}])
loadLogFile([{
"package": "extensions",
"name": "IgnoreTest",
"start": 1553898112395,
"end": 1553898112395,
"result": "skipped"
}])
loadLogFile([{
"package": "extensions",
"name": "RetryTest",
"start": 1553898112403,
"end": 1553898112405,
"result": "passed",
"attachments": [
]
}])
loadLogFile([{
"package": "extensions",
"name": "This title is easy to read for humans",
"start": 1553898112407,
"end": 1553898112408,
"result": "passed",
"attachments": [
]
}])
loadLogFile([{
"package": "extensions",
"name": "SeeTest",
"start": 1553898112409,
"end": 1553898112411,
"result": "passed",
"attachments": [
]
}])
loadLogFile([{
"package": "extensions",
"name": "StepwiseTest",
"start": 1553898112422,
"end": 1553898112423,
"result": "passed",
"attachments": [
]
}])
loadLogFile([{
"package": "extensions",
"name": "NarrativeDescriptionTest",
"start": 1553898112427,
"narrative": "as a user\n i want to save favourite items \n and then get the list of them",
"end": 1553898112433,
"result": "passed",
"attachments": [
]
}])
loadLogFile([{
"package": "extensions",
"name": "SubjectTest",
"start": 1553898112434,
"end": 1553898112436,
"result": "passed",
"attachments": [
]
}])
loadLogFile([{
"package": "extensions",
"name": "IgnoreRestTest",
"start": 1553898112437,
"end": 1553898112437,
"result": "passed",
"attachments": [
]
}])
loadLogFile([{
"package": "extensions",
"name": "StackTraceTest",
"start": 1553898112438,
"features": [
{
"name": "stacktrace",
"start": 1553898112438,
"exceptions": [
"java.lang.RuntimeException: blabla\n\tat extensions.StackTraceTest.stacktrace(StackTraceTest.groovy:10)\n"
],
"end": 1553898112455,
"result": "failed",
"attachments": [
]
}
],
"end": 1553898112470,
"result": "failed",
"attachments": [
]
}])
loadLogFile([{
"package": "extensions",
"name": "IgnoreIfTest",
"start": 1553898112471,
"end": 1553898112472,
"result": "passed",
"attachments": [
]
}])
loadLogFile([{
"package": "extensions",
"name": "RequiresTest",
"start": 1553898112473,
"features": [
{
"name": "I will run only on Windows",
"start": 1553898112474,
"end": 1553898112474,
"result": "skipped"
}
],
"end": 1553898112476,
"result": "passed",
"attachments": [
]
}])
loadLogFile([{
"package": "extensions",
"name": "IssueTest",
"start": 1553898112477,
"features": [
{
"name": "I'm using Spock configuration file",
"start": 1553898112477,
"tags": [
{
"name": "Bug LO-1000",
"key": "issue",
"value": "LO-1000",
"url": "http:\/\/jira.org\/issues\/LO-1000"
}
],
"end": 1553898112489,
"result": "passed",
"attachments": [
]
}
],
"end": 1553898112490,
"result": "passed",
"attachments": [
]
}])
loadLogFile([{
"package": "extensions",
"name": "TimeoutTest",
"start": 1553898112491,
"features": [
{
"name": "I will fail after 200 millis",
"start": 1553898112491,
"end": 1553898112514,
"result": "passed",
"attachments": [
]
}
],
"end": 1553898112517,
"result": "passed",
"attachments": [
]
}])
loadLogFile([{
"package": "extensions",
"name": "RestoreSystemPropertiesTest",
"start": 1553898112518,
"features": [
{
"name": "all environment variables will be saved before execution and restored after tests",
"start": 1553898112518,
"end": 1553898112532,
"result": "passed",
"attachments": [
]
}
],
"end": 1553898112539,
"result": "passed",
"attachments": [
]
}])

View File

@@ -1,7 +0,0 @@
package mocks;
public interface EventPublisher {
void publish(String addedOfferId);
}

View File

@@ -1,5 +0,0 @@
package mocks;
public class ExternalItemProviderException extends RuntimeException {
}

View File

@@ -1,36 +0,0 @@
package mocks;
import java.util.Objects;
public class Item {
private final String id;
private final String name;
public Item(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Item item = (Item) o;
return Objects.equals(id, item.id) &&
Objects.equals(name, item.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
}

View File

@@ -1,9 +0,0 @@
package mocks;
import java.util.List;
public interface ItemProvider {
List<Item> getItems(List<String> itemIds);
}

View File

@@ -1,37 +0,0 @@
package mocks;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class ItemService {
private final ItemProvider itemProvider;
private final EventPublisher eventPublisher;
public ItemService(ItemProvider itemProvider, EventPublisher eventPublisher) {
this.itemProvider = itemProvider;
this.eventPublisher = eventPublisher;
}
List<Item> getAllItemsSortedByName(List<String> itemIds) {
List<Item> items;
try{
items = itemProvider.getItems(itemIds);
} catch (RuntimeException ex) {
throw new ExternalItemProviderException();
}
return items.stream()
.sorted(Comparator.comparing(Item::getName))
.collect(Collectors.toList());
}
void saveItems(List<String> itemIds) {
List<String> notEmptyOfferIds = itemIds.stream()
.filter(itemId -> !itemId.isEmpty())
.collect(Collectors.toList());
// save in database
notEmptyOfferIds.forEach(eventPublisher::publish);
}
}

View File

@@ -1,10 +0,0 @@
package mocks;
public class LoggingEventPublisher implements EventPublisher {
@Override
public void publish(String addedOfferId) {
System.out.println("I've published: " + addedOfferId);
}
}

View File

@@ -1,89 +0,0 @@
import spock.lang.Specification
class FirstSpecification extends Specification {
def "one plus one should equal two"() {
expect:
1 + 1 == 2
}
def "two plus two should equal four"() {
given:
int left = 2
int right = 2
when:
int result = left + right
then:
result == 4
}
def "Should be able to remove from list"() {
given:
def list = [1, 2, 3, 4]
when:
list.remove(0)
then:
list == [2, 3, 4]
}
def "Should get an index out of bounds when removing a non-existent item"() {
given:
def list = [1, 2, 3, 4]
when:
list.remove(20)
then:
thrown(IndexOutOfBoundsException)
list.size() == 4
}
def "numbers to the power of two"(int a, int b, int c) {
expect:
Math.pow(a, b) == c
where:
a | b | c
1 | 2 | 1
2 | 2 | 4
3 | 2 | 9
}
def "Should return default value for mock"() {
given:
def paymentGateway = Mock(PaymentGateway)
when:
def result = paymentGateway.makePayment(12.99)
then:
!result
}
def "Should return true value for mock"() {
given:
def paymentGateway = Mock(PaymentGateway)
paymentGateway.makePayment(20) >> true
when:
def result = paymentGateway.makePayment(20)
then:
result
}
def "Should verify notify was called"() {
given:
def notifier = Mock(Notifier)
when:
notifier.notify('foo')
then:
1 * notifier.notify('foo')
}
}

View File

@@ -1,3 +0,0 @@
interface Notifier {
void notify(String message)
}

View File

@@ -1,3 +0,0 @@
interface PaymentGateway {
boolean makePayment(BigDecimal amount)
}

View File

@@ -1,20 +0,0 @@
package extensions
import spock.lang.Narrative
import spock.lang.Specification
import spock.lang.Title
@Title("""This title is easy to read for humans""")
class CustomTitleTest extends Specification {
}
@Narrative("""
as a user
i want to save favourite items
and then get the list of them
""")
class NarrativeDescriptionTest extends Specification {
}

View File

@@ -1,22 +0,0 @@
package extensions
import spock.lang.IgnoreIf
import spock.lang.Specification
class IgnoreIfTest extends Specification {
@IgnoreIf({System.getProperty("os.name").contains("windows")})
def "I won't run on windows"() {
expect:
true
}
@IgnoreIf({ os.isWindows() })
def "I'm using Spock helper classes to run only on windows"() {
expect:
true
}
}

View File

@@ -1,16 +0,0 @@
package extensions
import spock.lang.IgnoreRest
import spock.lang.Specification
class IgnoreRestTest extends Specification {
def "I won't run"() { }
@IgnoreRest
def 'I will run'() { }
def "I won't run too"() { }
}

View File

@@ -1,20 +0,0 @@
package extensions
import spock.lang.Ignore
import spock.lang.Specification
@Ignore
class IgnoreTest extends Specification {
@Ignore
def "I won't be executed"() {
expect:
true
}
def 'Example test'() {
expect:
true
}
}

View File

@@ -1,25 +0,0 @@
package extensions
import spock.lang.Issue
import spock.lang.Specification
class IssueTest extends Specification {
@Issue("http://jira.org/issues/LO-531")
def 'single issue'() {
}
@Issue(["http://jira.org/issues/LO-531", "http://jira.org/issues/LO-123"])
def 'multiple issues'() {
}
@Issue("LO-1000")
def "I'm using Spock configuration file"() {
expect:
true
}
}

View File

@@ -1,11 +0,0 @@
package extensions
import spock.lang.PendingFeature
class PendingFeatureTest {
@PendingFeature
def 'test for not implemented yet feature'() {
}
}

View File

@@ -1,14 +0,0 @@
package extensions
import spock.lang.Requires
import spock.lang.Specification
class RequiresTest extends Specification {
@Requires({ System.getProperty("os.name").contains("windows") })
def "I will run only on Windows"() {
expect:
true
}
}

View File

@@ -1,18 +0,0 @@
package extensions
import spock.lang.Specification
import spock.util.environment.RestoreSystemProperties
class RestoreSystemPropertiesTest extends Specification {
@RestoreSystemProperties
def 'all environment variables will be saved before execution and restored after tests'() {
given:
System.setProperty('os.name', 'Mac OS')
expect:
true
}
}

View File

@@ -1,20 +0,0 @@
package extensions
import spock.lang.Retry
import spock.lang.Specification
@Retry
class RetryTest extends Specification {
@Retry
def 'I will retry three times'() { }
@Retry(exceptions = [RuntimeException])
def 'I will retry only on RuntimeException'() { }
@Retry(condition = { failure.message.contains('error') })
def 'I will retry with a specific message'() { }
@Retry(delay = 1000)
def 'I will retry after 1000 millis'() { }
}

View File

@@ -1,20 +0,0 @@
package extensions
import spock.lang.See
import spock.lang.Specification
class SeeTest extends Specification {
@See("https://example.org")
def 'Look at the reference'() {
}
@See(["https://example.org/first", "https://example.org/first"])
def 'Look at the references'() {
}
}

View File

@@ -1,13 +0,0 @@
package extensions
import org.junit.Ignore
import spock.lang.Specification
class StackTraceTest extends Specification {
def 'stkacktrace'() {
// expect:
// throw new RuntimeException("blabla")
}
}

View File

@@ -1,14 +0,0 @@
package extensions
import spock.lang.Specification
import spock.lang.Stepwise
@Stepwise
class StepwiseTest extends Specification {
def 'I will run as first'() { }
def 'I will run as second'() { }
}

View File

@@ -1,13 +0,0 @@
package extensions
import mocks.ItemService
import spock.lang.Specification
import spock.lang.Subject
class SubjectTest extends Specification {
@Subject
ItemService itemService // initialization here...
}

View File

@@ -1,24 +0,0 @@
package extensions
import spock.lang.Specification
import spock.lang.Timeout
import java.util.concurrent.TimeUnit
@Timeout(5)
class TimeoutTest extends Specification {
@Timeout(1)
def 'I have one second to finish'() {
}
def 'I will have 5 seconds timeout'() {}
@Timeout(value = 200, unit = TimeUnit.SECONDS)
def 'I will fail after 200 millis'() {
expect:
true
}
}

View File

@@ -1,131 +0,0 @@
package mocks
import spock.lang.Specification
class ItemServiceUnitTest extends Specification {
ItemProvider itemProvider
ItemService itemService
EventPublisher eventPublisher
def setup() {
itemProvider = Stub(ItemProvider)
eventPublisher = Mock(EventPublisher)
itemService = new ItemService(itemProvider, eventPublisher)
}
def 'should return items sorted by name'() {
given:
def ids = ['offer-id', 'offer-id-2']
itemProvider.getItems(ids) >> [new Item('offer-id-2', 'Zname'), new Item('offer-id', 'Aname')]
when:
List<Item> items = itemService.getAllItemsSortedByName(ids)
then:
items.collect { it.name } == ['Aname', 'Zname']
}
def 'arguments constraints'() {
itemProvider.getItems(['offer-id'])
itemProvider.getItems(_) >> []
itemProvider.getItems(*_) >> []
itemProvider.getItems(!null) >> []
itemProvider.getItems({ it.size > 0 }) >> []
}
def 'should return different items on subsequent call'() {
given:
itemProvider.getItems(_) >>> [
[],
[new Item('1', 'name')],
[new Item('2', 'name')]
]
when: 'method is called for the first time'
List<Item> items = itemService.getAllItemsSortedByName(['not-important'])
then: 'empty list is returned'
items == []
when: 'method is called for the second time'
items = itemService.getAllItemsSortedByName(['not-important'])
then: 'item with id=1 is returned'
items == [new Item('1', 'name')]
when: 'method is called for the thirdtime'
items = itemService.getAllItemsSortedByName(['not-important'])
then: 'item with id=2 is returned'
items == [new Item('2', 'name')]
}
def 'should throw ExternalItemProviderException when ItemProvider fails'() {
given:
itemProvider.getItems(_) >> { new RuntimeException()}
when:
itemService.getAllItemsSortedByName([])
then:
thrown(ExternalItemProviderException)
}
def 'chaining response'() {
itemProvider.getItems(_) >>> { new RuntimeException() } >> new SocketTimeoutException() >> [new Item('id', 'name')]
}
def 'should return different items for different ids lists'() {
given:
def firstIds = ['first']
def secondIds = ['second']
itemProvider.getItems(firstIds) >> [new Item('first', 'Zname')]
itemProvider.getItems(secondIds) >> [new Item('second', 'Aname')]
when:
def firstItems = itemService.getAllItemsSortedByName(firstIds)
def secondItems = itemService.getAllItemsSortedByName(secondIds)
then:
firstItems.first().name == 'Zname'
secondItems.first().name == 'Aname'
}
def 'should publish events about new non-empty saved offers'() {
given:
def offerIds = ['', 'a', 'b']
when:
itemService.saveItems(offerIds)
then:
2 * eventPublisher.publish({ it != null && !it.isEmpty()})
}
def 'should return items'() {
given:
itemProvider = Mock(ItemProvider)
itemProvider.getItems(['item-id']) >> [new Item('item-id', 'name')]
itemService = new ItemService(itemProvider, eventPublisher)
when:
def items = itemService.getAllItemsSortedByName(['item-id'])
then:
items == [new Item('item-id', 'name')]
}
def 'should spy on EventPublisher method call'() {
given:
LoggingEventPublisher eventPublisher = Spy(LoggingEventPublisher)
itemService = new ItemService(itemProvider, eventPublisher)
when:
itemService.saveItems(['item-id'])
then:
1 * eventPublisher.publish('item-id')
}
}

View File

@@ -1,30 +0,0 @@
import extensions.TimeoutTest
import spock.lang.Issue
runner {
if (System.getenv("FILTER_STACKTRACE") == null) {
filterStackTrace false
}
report {
issueNamePrefix 'Bug '
issueUrlPrefix 'http://jira.org/issues/'
}
optimizeRunOrder true
// exclude TimeoutTest
// exclude {
// baseClass TimeoutTest
// annotation Issue
// }
report {
enabled true
logFileDir '.'
logFileName 'report.json'
logFileSuffix new Date().format('yyyy-MM-dd')
}
}