Compare commits
19
Commits
af96b8ba92
...
0.5.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e67bdc4bb0 | ||
|
|
9a14ea6486 | ||
|
|
1549f85a49 | ||
|
|
efb644e6a9 | ||
|
|
6a3d7f4f42 | ||
|
|
6feb7f87dc | ||
|
|
47af8b4522 | ||
|
|
db8fe1fc47 | ||
|
|
e803452389 | ||
|
|
08c85459a7 | ||
|
|
9edbca09cf | ||
|
|
b3a010f6e9 | ||
|
|
b1332c838b | ||
|
|
df1261f31c | ||
|
|
38a6eeb889 | ||
|
|
4136775a1f | ||
|
|
dabb09c518 | ||
|
|
c1846e60a4 | ||
|
|
b195d0af7a |
+24
@@ -0,0 +1,24 @@
|
|||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: gradle
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: test
|
||||||
|
image: gradle:8.3.0-jdk17-graal
|
||||||
|
commands:
|
||||||
|
- gradle assemble
|
||||||
|
- gradle check
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: maven
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: test
|
||||||
|
image: maven:3.9.4-eclipse-temurin-17-alpine
|
||||||
|
commands:
|
||||||
|
- mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
|
||||||
|
- mvn test -B
|
||||||
|
|
||||||
@@ -14,6 +14,12 @@ target/
|
|||||||
.settings/
|
.settings/
|
||||||
bin/
|
bin/
|
||||||
|
|
||||||
|
# Emacs
|
||||||
|
*~
|
||||||
|
.\#*
|
||||||
|
\#*\#
|
||||||
|
.*.~undo-tree~
|
||||||
|
|
||||||
# IDEA
|
# IDEA
|
||||||
*.iws
|
*.iws
|
||||||
*.ipr
|
*.ipr
|
||||||
|
|||||||
+4
-41
@@ -1,25 +1,16 @@
|
|||||||
[.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
|
== simple-jbdd
|
||||||
|
|
||||||
|
This project is a simple BDD framework.
|
||||||
|
|
||||||
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.
|
The project uses the https://github.com/spockframework/[Spock Framework Example Project^]
|
||||||
|
|
||||||
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
|
=== Prerequisites
|
||||||
|
|
||||||
- JDK 8 or higher
|
- JDK 17 or higher
|
||||||
- Maven use `mvnw` wrapper
|
- Maven use `mvnw` wrapper
|
||||||
- Gradle use `gradlew` 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
|
=== Building with Gradle
|
||||||
Type:
|
Type:
|
||||||
|
|
||||||
@@ -34,31 +25,3 @@ Type:
|
|||||||
|
|
||||||
Downloaded files will be stored in the local Maven repository (typically *user_home*`/.m2/repository`).
|
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
|
|
||||||
|
|
||||||
|
|||||||
+2
-8
@@ -3,20 +3,18 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
version = "1.0"
|
version = "1.0"
|
||||||
description = "Spock Framework - Example Project"
|
description = "simple-jbdd is a simple BDD testing framework for Java."
|
||||||
|
|
||||||
// Spock works with Java 1.8 and above
|
// Spock works with Java 1.8 and above
|
||||||
java {
|
java {
|
||||||
toolchain {
|
toolchain {
|
||||||
languageVersion.set(JavaLanguageVersion.of(8))
|
languageVersion.set(JavaLanguageVersion.of(17))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
// Spock releases are available from Maven Central
|
// Spock releases are available from Maven Central
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
// Spock snapshots are available from the Sonatype OSS snapshot repository
|
|
||||||
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
@@ -31,10 +29,6 @@ dependencies {
|
|||||||
testImplementation "org.hamcrest:hamcrest-core:2.2" // only necessary if Hamcrest matchers are used
|
testImplementation "org.hamcrest:hamcrest-core:2.2" // only necessary if Hamcrest matchers are used
|
||||||
testRuntimeOnly 'net.bytebuddy:byte-buddy:1.14.8' // allows mocking of classes (in addition to interfaces)
|
testRuntimeOnly 'net.bytebuddy:byte-buddy:1.14.8' // allows mocking of classes (in addition to interfaces)
|
||||||
testRuntimeOnly "org.objenesis:objenesis:3.3" // allows mocking of classes without default constructor (together with ByteBuddy or CGLIB)
|
testRuntimeOnly "org.objenesis:objenesis:3.3" // allows mocking of classes without default constructor (together with ByteBuddy or CGLIB)
|
||||||
|
|
||||||
// dependencies used by examples in this project
|
|
||||||
testRuntimeOnly "com.h2database:h2:2.2.220"
|
|
||||||
implementation "org.apache.groovy:groovy-sql"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
test {
|
test {
|
||||||
|
|||||||
@@ -2,11 +2,11 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<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">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.spockframework</groupId>
|
<groupId>space.kklochko</groupId>
|
||||||
<artifactId>spock-example</artifactId>
|
<artifactId>simple_jbdd</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<name>Spock Framework - Example Project</name>
|
<name>simple-jbdd</name>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
@@ -56,7 +56,7 @@
|
|||||||
<!-- Only required for spock-example build -->
|
<!-- Only required for spock-example build -->
|
||||||
<plugin>
|
<plugin>
|
||||||
<artifactId>maven-deploy-plugin</artifactId>
|
<artifactId>maven-deploy-plugin</artifactId>
|
||||||
<version>3.0.0</version>
|
<version>3.1.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<skip>true</skip>
|
<skip>true</skip>
|
||||||
</configuration>
|
</configuration>
|
||||||
@@ -116,7 +116,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.h2database</groupId>
|
<groupId>com.h2database</groupId>
|
||||||
<artifactId>h2</artifactId>
|
<artifactId>h2</artifactId>
|
||||||
<version>2.2.220</version>
|
<version>2.2.222</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.groovy</groupId>
|
<groupId>org.apache.groovy</groupId>
|
||||||
@@ -124,27 +124,4 @@
|
|||||||
<version>${groovy.version}</version>
|
<version>${groovy.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<repositories>
|
|
||||||
<!-- Only required if a snapshot version of Spock is used -->
|
|
||||||
<repository>
|
|
||||||
<id>spock-snapshots</id>
|
|
||||||
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
|
|
||||||
<snapshots>
|
|
||||||
<enabled>true</enabled>
|
|
||||||
</snapshots>
|
|
||||||
</repository>
|
|
||||||
</repositories>
|
|
||||||
|
|
||||||
<!-- Only required in spock-example build -->
|
|
||||||
<distributionManagement>
|
|
||||||
<repository>
|
|
||||||
<id>foo</id>
|
|
||||||
<url>file:///fake.repository.to.make.maven.happy</url>
|
|
||||||
</repository>
|
|
||||||
<snapshotRepository>
|
|
||||||
<id>bar</id>
|
|
||||||
<url>file:///fake.repository.to.make.maven.happy</url>
|
|
||||||
</snapshotRepository>
|
|
||||||
</distributionManagement>
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
rootProject.name = "spock-example"
|
rootProject.name = "simple_bdd"
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package space.kklochko.simple_jbdd.annotations;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
public @interface Given {
|
||||||
|
String value() default "Given";
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package space.kklochko.simple_jbdd.annotations;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
public @interface Then {
|
||||||
|
String value() default "Then";
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package space.kklochko.simple_jbdd.annotations;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
public @interface Title {
|
||||||
|
String value() default "Title";
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package space.kklochko.simple_jbdd.annotations;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
public @interface When {
|
||||||
|
String value() default "When";
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package space.kklochko.simple_jbdd.test_examples.samples;
|
||||||
|
|
||||||
|
public class Sum {
|
||||||
|
public static int sum(int a, int b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
package space.kklochko.simple_jbdd.test_examples.tests;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Then;
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Title;
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
|
||||||
|
|
||||||
|
@Title("A Simple Failed GivenWhenTest for Sum")
|
||||||
|
public class SimpleFailedThenTest extends Test {
|
||||||
|
@Then
|
||||||
|
public void check() {
|
||||||
|
assert 30 == 10 + 20 + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
package space.kklochko.simple_jbdd.test_examples.tests;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Given;
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Then;
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Title;
|
||||||
|
import space.kklochko.simple_jbdd.annotations.When;
|
||||||
|
import space.kklochko.simple_jbdd.test_examples.samples.Sum;
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
|
||||||
|
@Title("A Simple GivenWhenTest for Sum")
|
||||||
|
public class SimpleGivenWhenThenTest extends Test {
|
||||||
|
private Sum sum;
|
||||||
|
private int a;
|
||||||
|
private int b;
|
||||||
|
|
||||||
|
@Given
|
||||||
|
void setup() {
|
||||||
|
sum = new Sum();
|
||||||
|
}
|
||||||
|
|
||||||
|
@When
|
||||||
|
void trigger() {
|
||||||
|
a = 10;
|
||||||
|
b = 20;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Then
|
||||||
|
void check() {
|
||||||
|
assert 30 == sum.sum(a, b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package space.kklochko.simple_jbdd.test_examples.tests;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Then;
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Title;
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
|
||||||
|
@Title("Simple test")
|
||||||
|
public class SimpleThenTest extends Test {
|
||||||
|
@Then("The sum of 10 and 20 is 30.")
|
||||||
|
public void check() {
|
||||||
|
assert 30 == 10 + 20;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package space.kklochko.simple_jbdd.test_examples.tests;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Then;
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Title;
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
|
||||||
|
public class SimpleThenTestWithoutTitle extends Test {
|
||||||
|
@Then("The sum of 10 and 20 is 30.")
|
||||||
|
public void check() {
|
||||||
|
assert 30 == 10 + 20;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests;
|
||||||
|
|
||||||
|
public abstract class Test {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests.commands;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
public abstract class AbstractTestCommand<T extends Test> {
|
||||||
|
protected String type;
|
||||||
|
protected String label;
|
||||||
|
|
||||||
|
public AbstractTestCommand(String label) {
|
||||||
|
setLabel(label);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract boolean test();
|
||||||
|
|
||||||
|
public Map<String, String> getLabels() {
|
||||||
|
Map<String, String> labels = new TreeMap<String, String>();
|
||||||
|
labels.put(getType(), this.getLabel());
|
||||||
|
return labels;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLabel(String label) {
|
||||||
|
this.label = label;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLabel() {
|
||||||
|
return label;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void setObject(T object);
|
||||||
|
|
||||||
|
public abstract T getObject();
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests.commands;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
|
||||||
|
public class SimpleTestCommand<T extends Test> extends AbstractTestCommand<T> {
|
||||||
|
protected T object;
|
||||||
|
|
||||||
|
public SimpleTestCommand(String label, T object) {
|
||||||
|
super(label);
|
||||||
|
setObject(object);
|
||||||
|
setType("Title");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean test() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setObject(T object) {
|
||||||
|
this.object = object;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getObject() {
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+55
@@ -0,0 +1,55 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests.commands.decorators;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
import space.kklochko.simple_jbdd.tests.commands.AbstractTestCommand;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public abstract class AbstractDecorator<T extends Test> extends AbstractTestCommand<T> {
|
||||||
|
protected AbstractTestCommand<T> command;
|
||||||
|
|
||||||
|
protected Method method;
|
||||||
|
|
||||||
|
public AbstractDecorator(String label, AbstractTestCommand<T> command) {
|
||||||
|
super(label);
|
||||||
|
setCommand(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract boolean test();
|
||||||
|
|
||||||
|
public Map<String, String> getLabels() {
|
||||||
|
Map<String, String> labels = getCommand().getLabels();
|
||||||
|
labels.put(getType(), this.getLabel());
|
||||||
|
return labels;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract boolean runMethod();
|
||||||
|
|
||||||
|
public void setMethod(Method method) {
|
||||||
|
this.method = method;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Method getMethod() {
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setObject(T object) {
|
||||||
|
command.setObject(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T getObject() {
|
||||||
|
return command.getObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
public AbstractTestCommand<T> getCommand() {
|
||||||
|
return command;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCommand(AbstractTestCommand<T> command) {
|
||||||
|
this.command = command;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+44
@@ -0,0 +1,44 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests.commands.decorators;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
import space.kklochko.simple_jbdd.tests.commands.AbstractTestCommand;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
public class BlockDecorator<T extends Test> extends AbstractDecorator<T> {
|
||||||
|
public BlockDecorator(String label, AbstractTestCommand<T> command, Method method, String type) {
|
||||||
|
super(label, command);
|
||||||
|
setType(type);
|
||||||
|
setMethod(method);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean test() {
|
||||||
|
boolean result = runMethod();
|
||||||
|
|
||||||
|
if(!result)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return getCommand().test();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean runMethod() {
|
||||||
|
try {
|
||||||
|
method.setAccessible(true);
|
||||||
|
method.invoke(getCommand().getObject());
|
||||||
|
return true;
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
Throwable exception = e.getCause();
|
||||||
|
|
||||||
|
if(exception instanceof AssertionError)
|
||||||
|
throw (AssertionError) exception;
|
||||||
|
|
||||||
|
System.err.println(e.getMessage());
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
System.err.println("Check access modifiers.");
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests.factories;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Given;
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Then;
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Title;
|
||||||
|
import space.kklochko.simple_jbdd.annotations.When;
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
import space.kklochko.simple_jbdd.tests.commands.AbstractTestCommand;
|
||||||
|
import space.kklochko.simple_jbdd.tests.commands.SimpleTestCommand;
|
||||||
|
import space.kklochko.simple_jbdd.tests.commands.decorators.BlockDecorator;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
public class TestCommandFactory {
|
||||||
|
public AbstractTestCommand create(Test input) {
|
||||||
|
Class<?> aClass = input.getClass();
|
||||||
|
Map<String, Method> test_methods = this.getTestMethods(aClass);
|
||||||
|
|
||||||
|
AbstractTestCommand<Test> aTestCommand = createTest(aClass, input);
|
||||||
|
|
||||||
|
if(test_methods.containsKey("Then")) {
|
||||||
|
Method then = test_methods.get("Then");
|
||||||
|
String label = then.getAnnotation(Then.class).value();
|
||||||
|
aTestCommand = new BlockDecorator<Test>(label, aTestCommand, then, "Then");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(test_methods.containsKey("When")) {
|
||||||
|
Method when = test_methods.get("When");
|
||||||
|
String label = when.getAnnotation(When.class).value();
|
||||||
|
aTestCommand = new BlockDecorator<Test>(label, aTestCommand, when, "When");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(test_methods.containsKey("Given")) {
|
||||||
|
Method when = test_methods.get("Given");
|
||||||
|
String label = when.getAnnotation(Given.class).value();
|
||||||
|
aTestCommand = new BlockDecorator<Test>(label, aTestCommand, when, "Given");
|
||||||
|
}
|
||||||
|
|
||||||
|
return aTestCommand;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected AbstractTestCommand<Test> createTest(Class<?> aClass, Test input) {
|
||||||
|
Title aTitle = aClass.getAnnotation(Title.class);
|
||||||
|
String title = aClass.getName();
|
||||||
|
|
||||||
|
if(aTitle != null)
|
||||||
|
title = aTitle.value();
|
||||||
|
|
||||||
|
return new SimpleTestCommand<Test>(title, input);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected <T extends Test> Map<String, Method> getTestMethods(Class<?> aClass) {
|
||||||
|
Map<String, Method> test_methods = new TreeMap<String, Method>();
|
||||||
|
|
||||||
|
for(Method method : aClass.getDeclaredMethods()) {
|
||||||
|
Annotation[] annotations = method.getAnnotations();
|
||||||
|
|
||||||
|
if (annotations.length == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if(method.isAnnotationPresent(Given.class))
|
||||||
|
test_methods.put("Given", method);
|
||||||
|
|
||||||
|
if(method.isAnnotationPresent(When.class))
|
||||||
|
test_methods.put("When", method);
|
||||||
|
|
||||||
|
if(method.isAnnotationPresent(Then.class))
|
||||||
|
test_methods.put("Then", method);
|
||||||
|
}
|
||||||
|
|
||||||
|
return test_methods;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests.runners;
|
||||||
|
|
||||||
|
public abstract class AbstractTestRunner {
|
||||||
|
public abstract boolean run();
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests.runners;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
import space.kklochko.simple_jbdd.tests.commands.AbstractTestCommand;
|
||||||
|
|
||||||
|
public class SimpleTestRunner extends AbstractTestRunner {
|
||||||
|
AbstractTestCommand<Test> command;
|
||||||
|
|
||||||
|
public SimpleTestRunner(AbstractTestCommand<Test> command) {
|
||||||
|
this.command = command;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean run() {
|
||||||
|
try {
|
||||||
|
return getCommand().test();
|
||||||
|
}catch (AssertionError e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public AbstractTestCommand<Test> getCommand() {
|
||||||
|
return command;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCommand(AbstractTestCommand<Test> command) {
|
||||||
|
this.command = command;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -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,37 +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 groovy.sql.Sql
|
|
||||||
import spock.lang.Shared
|
|
||||||
import spock.lang.Specification
|
|
||||||
|
|
||||||
class DatabaseDrivenSpec extends Specification {
|
|
||||||
@Shared sql = Sql.newInstance("jdbc:h2:mem:", "org.h2.Driver")
|
|
||||||
|
|
||||||
// insert data (usually the database would already contain the data)
|
|
||||||
def setupSpec() {
|
|
||||||
sql.execute("create table maxdata (id int primary key, a int, b int, c int)")
|
|
||||||
sql.execute("insert into maxdata values (1, 3, 7, 7), (2, 5, 4, 5), (3, 9, 9, 9), (4, 2, -3, 2)")
|
|
||||||
}
|
|
||||||
|
|
||||||
def "Maximum of #a and #b is #c"() {
|
|
||||||
expect:
|
|
||||||
Math.max(a, b) == c
|
|
||||||
|
|
||||||
where:
|
|
||||||
[a, b, c] << sql.rows("select a, b, c from maxdata")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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'"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package space.kklochko.simple_jbdd.test_examples.samples
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.test_examples.samples.Sum
|
||||||
|
import spock.lang.Specification
|
||||||
|
|
||||||
|
class SumSpec extends Specification {
|
||||||
|
def "Adding two numbers"() {
|
||||||
|
expect: "Two plus three is five."
|
||||||
|
5 == Sum.sum(2, 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+96
@@ -0,0 +1,96 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests.factories
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.test_examples.tests.SimpleGivenWhenThenTest
|
||||||
|
import space.kklochko.simple_jbdd.test_examples.tests.SimpleThenTest
|
||||||
|
import space.kklochko.simple_jbdd.test_examples.tests.SimpleThenTestWithoutTitle
|
||||||
|
import space.kklochko.simple_jbdd.tests.factories.TestCommandFactory
|
||||||
|
import spock.lang.Narrative;
|
||||||
|
import spock.lang.Specification
|
||||||
|
import spock.lang.Subject
|
||||||
|
import spock.lang.Title
|
||||||
|
|
||||||
|
@Narrative("""The factory must generate a test command, so
|
||||||
|
those tests check if factory generate them right.
|
||||||
|
""")
|
||||||
|
@Title("Unit test for TestCommandFactory")
|
||||||
|
class TestCommandFactorySpec extends Specification {
|
||||||
|
def "If the map of methods have one test block."() {
|
||||||
|
given: "I have a TestCommandFactory object"
|
||||||
|
@Subject
|
||||||
|
def factory = new TestCommandFactory();
|
||||||
|
|
||||||
|
and: "I also have a SimpleThenTest object"
|
||||||
|
def simpleThenTest = new SimpleThenTest()
|
||||||
|
|
||||||
|
when: "If a test have only one block"
|
||||||
|
def map = factory.getTestMethods(simpleThenTest.getClass())
|
||||||
|
|
||||||
|
then: "the map size is 1 method"
|
||||||
|
1 == map.size()
|
||||||
|
}
|
||||||
|
|
||||||
|
def "createTest() sets the title to a class name if no Title annotation."() {
|
||||||
|
given: "I have a test factory"
|
||||||
|
@Subject
|
||||||
|
def factory = new TestCommandFactory()
|
||||||
|
|
||||||
|
and: "I have a test class without Title"
|
||||||
|
def testClass = new SimpleThenTestWithoutTitle()
|
||||||
|
|
||||||
|
when: "I build a test"
|
||||||
|
def test = factory.create(testClass)
|
||||||
|
|
||||||
|
then: "The test have title as the test class"
|
||||||
|
testClass.class.getName() == test.getLabels()['Title']
|
||||||
|
}
|
||||||
|
|
||||||
|
def "createTest() sets the title to a value of the Title annotation."() {
|
||||||
|
given: "I have a test factory"
|
||||||
|
@Subject
|
||||||
|
def factory = new TestCommandFactory()
|
||||||
|
|
||||||
|
and: "I have a test class with Title"
|
||||||
|
def testClass = new SimpleThenTest()
|
||||||
|
|
||||||
|
and: "and the title of the annotation"
|
||||||
|
def titleClass = space.kklochko.simple_jbdd.annotations.Title.class
|
||||||
|
def title = testClass.getClass().getAnnotation(titleClass).value()
|
||||||
|
|
||||||
|
when: "I build a test"
|
||||||
|
def test = factory.create(testClass)
|
||||||
|
|
||||||
|
then: "The test have title as the test class"
|
||||||
|
title == test.getLabels()['Title']
|
||||||
|
}
|
||||||
|
|
||||||
|
def "createTest() build a Test with only Then."() {
|
||||||
|
given: "I have a test factory"
|
||||||
|
@Subject
|
||||||
|
def factory = new TestCommandFactory()
|
||||||
|
|
||||||
|
and: "I have a test class with Title"
|
||||||
|
def testClass = new SimpleThenTest()
|
||||||
|
|
||||||
|
when: "I build a test"
|
||||||
|
def test = factory.create(testClass)
|
||||||
|
|
||||||
|
then: "The test have title and then method"
|
||||||
|
['Title', 'Then'].toSet() == test.getLabels().keySet()
|
||||||
|
}
|
||||||
|
|
||||||
|
def "createTest() build a Test with Given-When-Then."() {
|
||||||
|
given: "I have a test factory"
|
||||||
|
@Subject
|
||||||
|
def factory = new TestCommandFactory()
|
||||||
|
|
||||||
|
and: "I have a test class with Title and Given-When-Then"
|
||||||
|
def testClass = new SimpleGivenWhenThenTest()
|
||||||
|
|
||||||
|
when: "I build a test"
|
||||||
|
def test = factory.create(testClass)
|
||||||
|
|
||||||
|
then: "The test have title and then method"
|
||||||
|
['Title', 'Given', 'When', 'Then'].toSet() == test.getLabels().keySet()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests.runners
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.test_examples.tests.SimpleFailedThenTest
|
||||||
|
import space.kklochko.simple_jbdd.test_examples.tests.SimpleThenTest
|
||||||
|
import space.kklochko.simple_jbdd.tests.commands.SimpleTestCommand
|
||||||
|
import space.kklochko.simple_jbdd.tests.factories.TestCommandFactory
|
||||||
|
import spock.lang.Narrative
|
||||||
|
import spock.lang.Specification
|
||||||
|
import spock.lang.Subject
|
||||||
|
import spock.lang.Title
|
||||||
|
|
||||||
|
@Narrative("""The runner must return a result of test, so
|
||||||
|
those tests check if passed and failed test generate the right result.
|
||||||
|
""")
|
||||||
|
@Title("Unit tests for SimpleTestRunner")
|
||||||
|
class SimpleTestRunnerSpec extends Specification {
|
||||||
|
def "The test with then has been passed."() {
|
||||||
|
given: "I have a testCommand"
|
||||||
|
def test = Stub(SimpleTestCommand)
|
||||||
|
test.test() >>> [true]
|
||||||
|
|
||||||
|
and: "I have a test runner"
|
||||||
|
@Subject
|
||||||
|
def runner = new SimpleTestRunner(test)
|
||||||
|
|
||||||
|
when: "The testCommand has been run"
|
||||||
|
def result = runner.run()
|
||||||
|
|
||||||
|
then: "The result is true"
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
def "The real test with then has been passed."() {
|
||||||
|
given: "I have a test and a factory"
|
||||||
|
def aTest = new SimpleThenTest()
|
||||||
|
def factory = new TestCommandFactory();
|
||||||
|
|
||||||
|
and: "I have a testCommand"
|
||||||
|
def test = factory.create(aTest)
|
||||||
|
|
||||||
|
and: "I have a test runner"
|
||||||
|
@Subject
|
||||||
|
def runner = new SimpleTestRunner(test)
|
||||||
|
|
||||||
|
when: "The testCommand has been run"
|
||||||
|
def result = runner.run()
|
||||||
|
|
||||||
|
then: "The result is true"
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
def "The test with then that has not been passed."() {
|
||||||
|
given: "I have a failed testCommand"
|
||||||
|
def test = Stub(SimpleTestCommand)
|
||||||
|
test.test() >> { assert 1 == 0 }
|
||||||
|
|
||||||
|
and: "I have a test runner"
|
||||||
|
@Subject
|
||||||
|
def runner = new SimpleTestRunner(test)
|
||||||
|
|
||||||
|
when: "The testCommand has been run"
|
||||||
|
def result = runner.run()
|
||||||
|
|
||||||
|
then: "The result is true"
|
||||||
|
!result
|
||||||
|
}
|
||||||
|
|
||||||
|
def "The real test with then has not been passed."() {
|
||||||
|
given: "I have a test and a factory"
|
||||||
|
def aTest = new SimpleFailedThenTest()
|
||||||
|
def factory = new TestCommandFactory();
|
||||||
|
|
||||||
|
and: "I have a testCommand"
|
||||||
|
def test = factory.create(aTest)
|
||||||
|
|
||||||
|
and: "I have a test runner"
|
||||||
|
@Subject
|
||||||
|
def runner = new SimpleTestRunner(test)
|
||||||
|
|
||||||
|
when: "The testCommand has been run"
|
||||||
|
def result = runner.run()
|
||||||
|
|
||||||
|
then: "The result is true"
|
||||||
|
!result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user