环境
- 🍎
Gradle
- 🍊
Spring Boot 2.3.6.RELEASE
- 🍍
Java 8
- 🥒
Groovy 3.0.6
- 🥥
Spock 2.0-M4-groovy-3.0
- 🍑
guava 30.0-jre
布局
父项目:parent
build.gradle
ext {//版本变量
guavaVersion = "30.0-jre"
spockVersion = "2.0-M4-groovy-3.0"
groovyVersion = "3.0.6"
}
//所有项目通用配置
allprojects {
apply plugin: 'java'
group = 'com.lugew.example-springboot-spock'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'//源码指定Java 8
targetCompatibility = '1.8'
//仓库
repositories {
mavenLocal()//本地优先级最高
maven {//阿里云
name '阿里云'
url 'https://maven.aliyun.com/repository/public/'
}
mavenCentral()//中央仓库
google()//google仓库
}
test {//使用JUnit测试平台
useJUnitPlatform()
}
}
//子项目通用配置
subprojects {
dependencies {
implementation "com.google.guava:guava:${guavaVersion}"//guava
testImplementation "org.codehaus.groovy:groovy-all:${groovyVersion}"//groovy
testImplementation "org.codehaus.groovy:groovy:${groovyVersion}"//groovy
testImplementation "org.spockframework:spock-core:${spockVersion}"//spock
}
}
setting.gradle
rootProject.name = 'parent'//父项目名称
includeFlat 'son'//平级子项目
子项目:son
build.gradle
plugins {
id 'org.springframework.boot' version '2.3.6.RELEASE'//springboot 插件
id 'io.spring.dependency-management' version '1.0.10.RELEASE'//springboot 依赖插件
id 'groovy'//groovy插件
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'//spring-boot-starter
implementation 'org.springframework.boot:spring-boot-starter-web'//web
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
setting.gradle
rootProject.name = 'son'
源码
groovy
java
JUnit
测试代码所在位置
例子
JUnit
以加法器Adder为例:
package com.lugew.examplespringbootspock.son.adder;
/**
* 加法
*
* @author LuGew
* @since 2020/12/8
*/
public class Adder {
public double add(double a, double b) {//加法
return a + b;
}
}
测试
AdderSpec
类
package com.lugew.examplespringbootspock.son.adder
import spock.lang.Specification
/**
* @author LuGew*
* @since 2020/12/8
*/
class AdderSpec extends Specification {
def "加法返回正确值 #a+#b=#c"() {
given: "初始化加法器"
Adder adder = new Adder()
expect: "返回正确值"
adder.add(a, b) == c
where: "例子"
a | b || c
1 | 1 || 2
2 | 1 || 3
2 | 0 || 2
0 | 0 || 0
1 | -1 || 0
}
}