add a tiny demo for spock

This commit is contained in:
金戟 2021-04-04 14:24:31 +08:00
parent f5475aa18e
commit e2e986e50d
9 changed files with 213 additions and 2 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
# maven ignore
target/
.mvn/
# gradle ignore
build/

View File

@ -12,6 +12,7 @@
<modules>
<module>java-demo</module>
<module>kotlin-demo</module>
<module>spock-demo</module>
</modules>
</project>

5
demo/settings.gradle Normal file
View File

@ -0,0 +1,5 @@
rootProject.name = 'demos'
include 'java-demo'
include 'kotlin-demo'
include 'spock-demo'

View File

@ -0,0 +1,28 @@
plugins {
id 'groovy'
}
group = 'com.alibaba.testable'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '8'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
testImplementation 'org.codehaus.groovy:groovy-all:3.0.7'
testImplementation 'org.spockframework:spock-core:2.0-M5-groovy-3.0'
testImplementation('com.alibaba.testable:testable-all:0.6.0')
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.6.0')
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
test {
jvmArgs "-javaagent:${classpath.find { it.name.contains("testable-agent") }.absolutePath}"
useJUnitPlatform()
}

86
demo/spock-demo/pom.xml Normal file
View File

@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Refer to: https://github.com/spockframework/spock-example/blob/master/pom.xml -->
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba.testable</groupId>
<artifactId>spock-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>spock-demo</name>
<description>Spock demo for Testable</description>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<testable.version>0.6.0</testable.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-bom</artifactId>
<version>2.0-M5-groovy-3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-all</artifactId>
<version>${testable.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- The gmavenplus plugin is used to compile Groovy code -->
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.12.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<argLine>-javaagent:${settings.localRepository}/com/alibaba/testable/testable-agent/${testable.version}/testable-agent-${testable.version}.jar</argLine>
<useFile>false</useFile>
<includes>
<include>**/*Test.java</include>
<include>**/*Spec.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,19 @@
package com.github.pbetkier.spockdemo;
import com.github.pbetkier.spockdemo.model.SpockBox;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
public class DemoSpock {
public SpockBox createBoxOfNum() {
SpockBox box = new SpockBox();
for (int i = 1; i <= 3; i++) {
box.put(String.valueOf(i));
}
return box;
}
}

View File

@ -0,0 +1,27 @@
package com.github.pbetkier.spockdemo.model;
import java.util.ArrayList;
import java.util.List;
public class SpockBox {
private List<String> contents = new ArrayList<>();
public int size() {
return contents.size();
}
public void put(String data) {
contents.add(data);
}
public String pop() {
if (contents.isEmpty()) {
return null;
}
String data = contents.get(size() - 1);
contents.remove(size() - 1);
return data;
}
}

View File

@ -0,0 +1,44 @@
package com.github.pbetkier.spockdemo
import com.alibaba.testable.core.annotation.MockConstructor
import com.alibaba.testable.core.annotation.MockMethod
import com.github.pbetkier.spockdemo.model.SpockBox
import spock.lang.Shared
import spock.lang.Specification
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
class DemoSpockTest extends Specification {
@Shared
def demoSpock = new DemoSpock()
static class Mock {
@MockConstructor
SpockBox createBox() {
SpockBox box = new SpockBox()
box.put("mock zero")
return box
}
@MockMethod(targetMethod = "put")
void putBox(SpockBox self, String data) {
self.put("mock " + data)
}
}
def "should get a box of numbers"() {
given:
def box = demoSpock.createBoxOfNum()
expect:
box.size() == 4
box.pop() == "mock 3"
box.pop() == "mock 2"
box.pop() == "mock 1"
box.pop() == "mock zero"
verify("createBox").withTimes(1)
verify("putBox").withInOrder("1").withInOrder("2").withInOrder("3")
}
}

View File

@ -9,10 +9,10 @@ read -p "Next version should be: " NEXT
for pom in testable-all/pom.xml testable-maven-plugin/pom.xml testable-processor/pom.xml testable-agent/pom.xml testable-core/pom.xml testable-parent/pom.xml; do
sed -i '' "s/<version>${VERSION}<\/version>/<version>${NEXT}<\/version>/" $pom
done
for gradle in demo/java-demo/build.gradle demo/kotlin-demo/build.gradle.kts; do
for gradle in demo/java-demo/build.gradle demo/kotlin-demo/build.gradle.kts demo/spock-demo/build.gradle; do
sed -i '' "s/testable-\([a-z]*\):${VERSION}/testable-\1:${NEXT}/" $gradle
done
for pom in testable-parent/pom.xml demo/java-demo/pom.xml demo/kotlin-demo/pom.xml; do
for pom in testable-parent/pom.xml demo/java-demo/pom.xml demo/kotlin-demo/pom.xml demo/spock-demo/pom.xml; do
sed -i '' "s/<testable.version>${VERSION}<\/testable.version>/<testable.version>${NEXT}<\/testable.version>/" $pom
done
for md in docs/zh-cn/doc/setup.md docs/en-us/doc/setup.md; do