parent
c1846e60a4
commit
e2c6f84d8d
@ -1,64 +0,0 @@
|
||||
[.float-group]
|
||||
--
|
||||
image::https://img.shields.io/badge/License-Apache%202.0-blue.svg[link=https://github.com/spockframework/spock/blob/master/LICENSE,float=left]
|
||||
image::https://github.com/spockframework/spock-example/actions/workflows/main.yml/badge.svg[link=https://github.com/spockframework/spock-example/actions/workflows/main.yml,float=left]
|
||||
image::https://badges.gitter.im/spockframework/spock.svg[link=https://gitter.im/spockframework/spock?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge,float=left]
|
||||
--
|
||||
|
||||
== Spock Framework Example Project
|
||||
|
||||
|
||||
The purpose of this project is to help you get started with Spock. The project includes several example specifications and build scripts for Gradle and Maven. It also makes it easy to create an Eclipse or IDEA project, allowing you to run the example specs from within your IDE.
|
||||
|
||||
All builds (Gradle and Maven) will automatically download all required dependencies, compile the project, and finally run the example specs. The Gradle build goes one step further by bootstrapping itself, alleviating the need to have a build tool preinstalled.
|
||||
|
||||
=== Prerequisites
|
||||
|
||||
- JDK 8 or higher
|
||||
- Maven use `mvnw` wrapper
|
||||
- Gradle use `gradlew` wrapper
|
||||
|
||||
NOTE: This example shows the usage of Spock 2.0, which uses the JUnit Platform. If you want to see how to get Spock 1.x with JUnit 4 up and running see the https://github.com/spockframework/spock-example/tree/spock-1.x[Spock-1.x] Branch.
|
||||
|
||||
=== Building with Gradle
|
||||
Type:
|
||||
|
||||
./gradlew clean test
|
||||
|
||||
Downloaded files (including the Gradle distribution itself) will be stored in the Gradle user home directory (typically *user_home*`/.gradle`).
|
||||
|
||||
=== Building with Maven
|
||||
Type:
|
||||
|
||||
./mvnw clean test
|
||||
|
||||
Downloaded files will be stored in the local Maven repository (typically *user_home*`/.m2/repository`).
|
||||
|
||||
=== Creating an Eclipse project
|
||||
|
||||
Install the https://projects.eclipse.org/projects/tools.buildship[Buildship plugin] if you want to use gradle as build tool.
|
||||
|
||||
=== Creating an IDEA project
|
||||
Just open the project directory with Intelli IDEA and it should auto-detect the project settings.
|
||||
|
||||
=== Further Resources
|
||||
|
||||
|
||||
* https://spockframework.org[Spock homepage]
|
||||
* https://meetspock.appspot.com[Spock web console]
|
||||
* https://docs.spockframework.org/[Main documentation]
|
||||
* https://stackoverflow.com/questions/tagged/spock[Spock on Stackoverflow]
|
||||
* https://gitter.im/spockframework/spock[Spock Chat]
|
||||
* https://github.com/spockframework/spock/discussions[Discussion group]
|
||||
* https://issues.spockframework.org[Issue tracker]
|
||||
* https://twitter.com/spockframework[Spock on Twitter]
|
||||
* https://gradle.org[Gradle homepage]
|
||||
* https://groovy-lang.org/[Groovy homepage]
|
||||
* https://maven.apache.org[Maven homepage]
|
||||
|
||||
If you have any comments or questions, please direct them to the Spock discussion group. We appreciate all feedback!
|
||||
|
||||
Happy spec'ing!
|
||||
|
||||
The Spock Framework Team
|
||||
|
@ -1 +1 @@
|
||||
rootProject.name = "spock-example"
|
||||
rootProject.name = "jdbc_hospital_example"
|
@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.*
|
||||
|
||||
class DataDrivenSpec extends Specification {
|
||||
def "maximum of two numbers"() {
|
||||
expect:
|
||||
Math.max(a, b) == c
|
||||
|
||||
where:
|
||||
a << [3, 5, 9]
|
||||
b << [7, 4, 9]
|
||||
c << [7, 5, 9]
|
||||
}
|
||||
|
||||
def "minimum of #a and #b is #c"() {
|
||||
expect:
|
||||
Math.min(a, b) == c
|
||||
|
||||
where:
|
||||
a | b || c
|
||||
3 | 7 || 3
|
||||
5 | 4 || 4
|
||||
9 | 9 || 9
|
||||
}
|
||||
|
||||
def "#person.name is a #sex.toLowerCase() person"() {
|
||||
expect:
|
||||
person.getSex() == sex
|
||||
|
||||
where:
|
||||
person || sex
|
||||
new Person(name: "Fred") || "Male"
|
||||
new Person(name: "Wilma") || "Female"
|
||||
}
|
||||
|
||||
static class Person {
|
||||
String name
|
||||
String getSex() {
|
||||
name == "Fred" ? "Male" : "Female"
|
||||
}
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.Specification
|
||||
|
||||
import static spock.util.matcher.HamcrestMatchers.closeTo
|
||||
|
||||
/**
|
||||
* Hamcrest matchers are deeply integrated with Spock's condition mechanism.
|
||||
* The syntax for using a matcher in a condition is simple:
|
||||
* {@code <expected-value> <matcher>}. If the condition fails, both the usual
|
||||
* condition output and the matcher's output will be shown.
|
||||
*
|
||||
* @author Peter Niederwiser
|
||||
* @since 0.5
|
||||
*/
|
||||
class HamcrestMatchersSpec extends Specification {
|
||||
def "comparing two decimal numbers"() {
|
||||
def myPi = 3.14
|
||||
|
||||
expect:
|
||||
myPi closeTo(Math.PI, 0.01)
|
||||
}
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.Specification
|
||||
|
||||
class HelloSpockSpec extends Specification {
|
||||
def "length of Spock's and his friends' names"() {
|
||||
expect:
|
||||
name.size() == length
|
||||
|
||||
where:
|
||||
name | length
|
||||
"Spock" | 5
|
||||
"Kirk" | 4
|
||||
"Scotty" | 6
|
||||
}
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import java.lang.annotation.*
|
||||
|
||||
import spock.lang.Specification
|
||||
|
||||
/**
|
||||
* Shows how methods can be included and excluded from a spec run. For
|
||||
* demonstration purposes, the configuration script's location is set
|
||||
* programmatically in a static initializer. Usually, the configuration
|
||||
* script's location would be set externally (via the same system property),
|
||||
* or the configuration script would reside in one of the two default locations:
|
||||
* <li>
|
||||
* <ul><tt>SpockConfig.groovy</tt> on the class path</ul>
|
||||
* <ul><tt>$user_home/.spock/SpockConfig.groovy</tt> on the file system</ul>
|
||||
* </li>
|
||||
*
|
||||
* <p>Note that the configuration script referenced by the system property
|
||||
* may either reside on the class path or on the file system (make sure to
|
||||
* include the whole path).
|
||||
*
|
||||
* <p>What's not shown here is that filtering also works for classes.
|
||||
* Whereas methods can only be filtered based on annotations, classes can
|
||||
* also be filtered based on their base class.
|
||||
*
|
||||
* <p>If you directly want to see the effect of different Spock configurations,
|
||||
* just run this spec from your IDE with an additional VM parameter
|
||||
* <li>
|
||||
* <ul><tt>-Dspock.configuration=IncludeFastConfig.groovy</tt> or</ul>
|
||||
* <ul><tt>-Dspock.configuration=ExcludeSlowConfig.groovy</tt></ul>
|
||||
* </li>
|
||||
*
|
||||
* <p>See also the two corresponding resource files provided with this spec.
|
||||
*
|
||||
* @since 0.4
|
||||
*/
|
||||
class IncludeExcludeExtensionSpec extends Specification {
|
||||
@Fast
|
||||
def "a fast method"() {
|
||||
println "fast"
|
||||
expect: true
|
||||
}
|
||||
|
||||
@Slow
|
||||
def "a slow method"() {
|
||||
println "slow"
|
||||
expect: true
|
||||
}
|
||||
|
||||
def "a neither fast nor slow method"() {
|
||||
println "neither fast nor slow"
|
||||
expect: true
|
||||
}
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@interface Fast {}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@interface Slow {}
|
@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.*
|
||||
|
||||
/**
|
||||
* The expected order of interactions can now be specified by using multiple
|
||||
* then-blocks. Interactions in a later then-block must take place after
|
||||
* interactions in an earlier then-block. The order of interactions within
|
||||
* the same then-block is unspecified.
|
||||
*
|
||||
* @since 0.4
|
||||
*/
|
||||
class OrderedInteractionsSpec extends Specification {
|
||||
def "collaborators must be invoked in order"() {
|
||||
def coll1 = Mock(Collaborator)
|
||||
def coll2 = Mock(Collaborator)
|
||||
|
||||
when:
|
||||
// try to reverse the order of these invocations and see what happens
|
||||
coll1.collaborate()
|
||||
coll2.collaborate()
|
||||
|
||||
then:
|
||||
1 * coll1.collaborate()
|
||||
|
||||
then:
|
||||
1 * coll2.collaborate()
|
||||
}
|
||||
}
|
||||
|
||||
interface Collaborator {
|
||||
def collaborate()
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.Specification
|
||||
|
||||
class Publisher {
|
||||
def subscribers = []
|
||||
|
||||
def send(event) {
|
||||
subscribers.each {
|
||||
try {
|
||||
it.receive(event)
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface Subscriber {
|
||||
def receive(event)
|
||||
}
|
||||
|
||||
class PublisherSpec extends Specification {
|
||||
def pub = new Publisher()
|
||||
def sub1 = Mock(Subscriber)
|
||||
def sub2 = Mock(Subscriber)
|
||||
|
||||
def setup() {
|
||||
pub.subscribers << sub1 << sub2
|
||||
}
|
||||
|
||||
def "delivers events to all subscribers"() {
|
||||
when:
|
||||
pub.send("event")
|
||||
|
||||
then:
|
||||
1 * sub1.receive("event")
|
||||
1 * sub2.receive("event")
|
||||
}
|
||||
|
||||
def "can cope with misbehaving subscribers"() {
|
||||
sub1.receive(_) >> { throw new Exception() }
|
||||
|
||||
when:
|
||||
pub.send("event1")
|
||||
pub.send("event2")
|
||||
|
||||
then:
|
||||
1 * sub2.receive("event1")
|
||||
1 * sub2.receive("event2")
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.*
|
||||
|
||||
abstract class BaseSpec extends Specification {
|
||||
def x = { println 'base field initializer' }()
|
||||
|
||||
def setupSpec() { println 'base setupSpec()' }
|
||||
def cleanupSpec() { println 'base cleanupSpec()' }
|
||||
|
||||
def setup() { println 'base setup()' }
|
||||
def cleanup() { println 'base cleanup()' }
|
||||
|
||||
def baseSpecMethod() { setup: println 'base spec method' }
|
||||
}
|
||||
|
||||
class DerivedSpec extends BaseSpec {
|
||||
def y = { println 'derived field initializer' }()
|
||||
|
||||
def setupSpec() { println 'derived setupSpec()' }
|
||||
def cleanupSpec() { println 'derived cleanupSpec()' }
|
||||
|
||||
def setup() { println 'derived setup()' }
|
||||
def cleanup() { println 'derived cleanup()' }
|
||||
|
||||
def derivedSpecMethod() { setup: println 'derived spec method' }
|
||||
}
|
@ -1,120 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.Specification
|
||||
|
||||
class EmptyStackSpec extends Specification {
|
||||
def stack = new Stack()
|
||||
|
||||
def "size"() {
|
||||
expect: stack.size() == 0
|
||||
}
|
||||
|
||||
def "pop"() {
|
||||
when: stack.pop()
|
||||
then: thrown(EmptyStackException)
|
||||
}
|
||||
|
||||
def "peek"() {
|
||||
when: stack.peek()
|
||||
then: thrown(EmptyStackException)
|
||||
}
|
||||
|
||||
def "push"() {
|
||||
when:
|
||||
stack.push("elem")
|
||||
|
||||
then:
|
||||
stack.size() == old(stack.size()) + 1
|
||||
stack.peek() == "elem"
|
||||
}
|
||||
}
|
||||
|
||||
class StackWithOneElementSpec extends Specification {
|
||||
def stack = new Stack()
|
||||
|
||||
def setup() {
|
||||
stack.push("elem")
|
||||
}
|
||||
|
||||
def "size"() {
|
||||
expect: stack.size() == 1
|
||||
}
|
||||
|
||||
def "pop"() {
|
||||
when:
|
||||
def x = stack.pop()
|
||||
|
||||
then:
|
||||
x == "elem"
|
||||
stack.size() == 0
|
||||
}
|
||||
|
||||
def "peek"() {
|
||||
when:
|
||||
def x = stack.peek()
|
||||
|
||||
then:
|
||||
x == "elem"
|
||||
stack.size() == 1
|
||||
}
|
||||
|
||||
def "push"() {
|
||||
when:
|
||||
stack.push("elem2")
|
||||
|
||||
then:
|
||||
stack.size() == 2
|
||||
stack.peek() == "elem2"
|
||||
}
|
||||
}
|
||||
|
||||
class StackWithThreeElementsSpec extends Specification {
|
||||
def stack = new Stack()
|
||||
|
||||
def setup() {
|
||||
["elem1", "elem2", "elem3"].each { stack.push(it) }
|
||||
}
|
||||
|
||||
def "size"() {
|
||||
expect: stack.size() == 3
|
||||
}
|
||||
|
||||
def "pop"() {
|
||||
expect:
|
||||
stack.pop() == "elem3"
|
||||
stack.pop() == "elem2"
|
||||
stack.pop() == "elem1"
|
||||
stack.size() == 0
|
||||
}
|
||||
|
||||
def "peek"() {
|
||||
expect:
|
||||
stack.peek() == "elem3"
|
||||
stack.peek() == "elem3"
|
||||
stack.peek() == "elem3"
|
||||
stack.size() == 3
|
||||
}
|
||||
|
||||
def "push"() {
|
||||
when:
|
||||
stack.push("elem4")
|
||||
|
||||
then:
|
||||
stack.size() == 4
|
||||
stack.peek() == "elem4"
|
||||
}
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.*
|
||||
|
||||
/**
|
||||
* Demonstrates how {@code @Stepwise} causes a spec to be run in incremental steps.
|
||||
* Change a step's condition from {@literal true} to {@literal false}, and observe
|
||||
* how the remaining steps will be skipped automatically on the next run.
|
||||
* Also notice that if you run a single step (e.g. from the IDE's context menu),
|
||||
* all prior steps will also be run.
|
||||
*
|
||||
* <p>{@code @Stepwise} is particularly useful for higher-level specs whose
|
||||
* methods have logical dependencies.
|
||||
*
|
||||
* @since 0.4
|
||||
*/
|
||||
@Stepwise
|
||||
class StepwiseExtensionSpec extends Specification {
|
||||
def "step 1"() {
|
||||
expect: true // try to change this to 'false'
|
||||
}
|
||||
|
||||
def "step 2"() {
|
||||
expect: true
|
||||
}
|
||||
|
||||
def "step 3"() {
|
||||
expect: true
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import org.junit.Rule
|
||||
import org.junit.rules.TestName
|
||||
import spock.lang.Specification
|
||||
|
||||
class UsingJUnitRulesSpec extends Specification {
|
||||
@Rule TestName name
|
||||
|
||||
def "retrieve test name at runtime"() {
|
||||
println "entering '$name.methodName'"
|
||||
expect: 1 + 1 == 2
|
||||
println "leaving '$name.methodName'"
|
||||
}
|
||||
}
|
Loading…
Reference in new issue