快速体验

Spock 官网提供了 Web 控制台,可以直接在此控制台页面体验和尝试,点击 Run Script 运行。

本地运行

  1. 准备好开发环境,开发工具不限,只要能支持 gradle 和 maven 构建工具都可以,或者无需开发工具,使用构建工具命令行运行。

  2. 新建一个 gradle 或 maven 项目。

    • gradle

    build.gradle 文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
plugins {
id 'java'
id 'groovy'
}

group 'net.sudot'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
mavenLocal()
mavenCentral()
}

dependencies {
// Spock依赖
testCompile 'org.spockframework:spock-core:1.3-groovy-2.5'
}

// 标记源码目录,若执行时出现如下错误则需加上:
// no tests found for given includes xxxx
sourceSets {
main {
groovy {
srcDirs = ['src/main/groovy']
}
java {
srcDirs = ['src/main/java']
}
}
test {
groovy {
srcDirs = ['src/test/groovy']
}
java {
srcDirs = ['src/test/java']
}
}
}
  • maven项目

pom.xml 内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>net.sudot</groupId>
<artifactId>spock-examples</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<!-- Mandatory dependencies for using Spock -->
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.3-groovy-2.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- groovy编译插件 -->
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.8.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-parameters</compilerArgument>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/central</url>
</repository>
<repository>
<id>mvnrepository</id>
<name>mvnrepository</name>
<url>http://mvnrepository.com/</url>
</repository>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/central</url>
</pluginRepository>
<pluginRepository>
<id>mvnrepository</id>
<name>mvnrepository</name>
<url>http://mvnrepository.com/</url>
</pluginRepository>
</pluginRepositories>
</project>
  1. 编写单元测试代码

在项目根目录下的 src/test/groovy 文件夹中新建一个 groovy 文件,例如:

1
2
3
4
5
6
7
8
9
// src/test/groovy/HelloWord.groovy
import spock.lang.Specification

class HelloWord extends Specification {
def testHelloWord() {
expect:
Math.max(1, 2) == 2
}
}

也可参考项目:spock-examplessrc/test/groovy/HelloWord.groovy

  1. 运行单元测试

gradle 项目在项目根目录中执行命令 ./gradlew test

第一次执行可能要等很久

1
2
3
4
5
6
7
8
9
10
11
tangjialindeMacBook-Pro:spock-examples tangjialin$ ./gradlew test

> Task :test
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/Users/tangjialin/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy/2.5.4/86b94e2949bcff3a13b7ad200e4c5299b52ad994/groovy-2.5.4.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

BUILD SUCCESSFUL in 4s
4 actionable tasks: 4 executed

maven 项目在项目根目录中执行命令 ./mvnw clean test

第一次执行可能要等很久

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
tangjialindeMacBook-Pro:spock-examples tangjialin$ ./mvnw clean test
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for net.sudot:spock-examples:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 48, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ----------------------< net.sudot:spock-examples >----------------------
[INFO] Building spock-examples 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ spock-examples ---
[INFO] Deleting /Users/tangjialin/src/java/spock-examples/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ spock-examples ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ spock-examples ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /Users/tangjialin/src/java/spock-examples/target/classes
[INFO]
[INFO] --- gmavenplus-plugin:1.8.0:compile (default) @ spock-examples ---
[INFO] No sources specified for compilation. Skipping.
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ spock-examples ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ spock-examples ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/tangjialin/src/java/spock-examples/target/test-classes
[INFO]
[INFO] --- gmavenplus-plugin:1.8.0:compileTests (default) @ spock-examples ---
[INFO] Using isolated classloader, without GMavenPlus classpath.
[INFO] Using Groovy 2.5.4 to perform compileTests.
[INFO] Compiled 2 files.
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ spock-examples ---
[INFO] Surefire report directory: /Users/tangjialin/src/java/spock-examples/target/surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running net.sudot.spockexamples.service.StudentServiceSpockTest
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/Users/tangjialin/.m2/repository/org/codehaus/groovy/groovy/2.5.4/groovy-2.5.4.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.417 sec
Running net.sudot.spockexamples.service.StudentServiceJUnitTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.011 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.839 s
[INFO] Finished at: 2019-10-20T23:12:46+08:00
[INFO] ------------------------------------------------------------------------

IntelliJ IDEA 中运行:

打开文件 src/test/groovy/HelloWord.groovy,点击鼠标右键选择 Run 'HelloWord'