From e2e986e50d7b54f80ed24902700d230d1072c09b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sun, 4 Apr 2021 14:24:31 +0800 Subject: [PATCH] add a tiny demo for spock --- .gitignore | 1 + demo/pom.xml | 1 + demo/settings.gradle | 5 ++ demo/spock-demo/build.gradle | 28 ++++++ demo/spock-demo/pom.xml | 86 +++++++++++++++++++ .../github/pbetkier/spockdemo/DemoSpock.java | 19 ++++ .../pbetkier/spockdemo/model/SpockBox.java | 27 ++++++ .../pbetkier/spockdemo/DemoSpockTest.groovy | 44 ++++++++++ tool/bump-to-next-release.sh | 4 +- 9 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 demo/settings.gradle create mode 100644 demo/spock-demo/build.gradle create mode 100644 demo/spock-demo/pom.xml create mode 100644 demo/spock-demo/src/main/java/com/github/pbetkier/spockdemo/DemoSpock.java create mode 100644 demo/spock-demo/src/main/java/com/github/pbetkier/spockdemo/model/SpockBox.java create mode 100644 demo/spock-demo/src/test/groovy/com/github/pbetkier/spockdemo/DemoSpockTest.groovy diff --git a/.gitignore b/.gitignore index a3c337a..d7c0e6c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # maven ignore target/ +.mvn/ # gradle ignore build/ diff --git a/demo/pom.xml b/demo/pom.xml index bee3c09..b2e1c64 100755 --- a/demo/pom.xml +++ b/demo/pom.xml @@ -12,6 +12,7 @@ java-demo kotlin-demo + spock-demo diff --git a/demo/settings.gradle b/demo/settings.gradle new file mode 100644 index 0000000..028944b --- /dev/null +++ b/demo/settings.gradle @@ -0,0 +1,5 @@ +rootProject.name = 'demos' + +include 'java-demo' +include 'kotlin-demo' +include 'spock-demo' diff --git a/demo/spock-demo/build.gradle b/demo/spock-demo/build.gradle new file mode 100644 index 0000000..7ebed3d --- /dev/null +++ b/demo/spock-demo/build.gradle @@ -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() +} diff --git a/demo/spock-demo/pom.xml b/demo/spock-demo/pom.xml new file mode 100644 index 0000000..00a298c --- /dev/null +++ b/demo/spock-demo/pom.xml @@ -0,0 +1,86 @@ + + + + 4.0.0 + com.alibaba.testable + spock-demo + 1.0.0-SNAPSHOT + spock-demo + Spock demo for Testable + + 1.8 + 1.8 + 1.8 + 0.6.0 + + + + + + org.spockframework + spock-bom + 2.0-M5-groovy-3.0 + pom + import + + + + + + + org.codehaus.groovy + groovy + 3.0.7 + + + org.spockframework + spock-core + test + + + org.spockframework + spock-junit4 + test + + + com.alibaba.testable + testable-all + ${testable.version} + test + + + + + + + + org.codehaus.gmavenplus + gmavenplus-plugin + 1.12.0 + + + + compile + compileTests + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M5 + + -javaagent:${settings.localRepository}/com/alibaba/testable/testable-agent/${testable.version}/testable-agent-${testable.version}.jar + false + + **/*Test.java + **/*Spec.java + + + + + + + diff --git a/demo/spock-demo/src/main/java/com/github/pbetkier/spockdemo/DemoSpock.java b/demo/spock-demo/src/main/java/com/github/pbetkier/spockdemo/DemoSpock.java new file mode 100644 index 0000000..6b031b3 --- /dev/null +++ b/demo/spock-demo/src/main/java/com/github/pbetkier/spockdemo/DemoSpock.java @@ -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; + } + +} diff --git a/demo/spock-demo/src/main/java/com/github/pbetkier/spockdemo/model/SpockBox.java b/demo/spock-demo/src/main/java/com/github/pbetkier/spockdemo/model/SpockBox.java new file mode 100644 index 0000000..9a91abf --- /dev/null +++ b/demo/spock-demo/src/main/java/com/github/pbetkier/spockdemo/model/SpockBox.java @@ -0,0 +1,27 @@ +package com.github.pbetkier.spockdemo.model; + +import java.util.ArrayList; +import java.util.List; + +public class SpockBox { + + private List 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; + } + +} diff --git a/demo/spock-demo/src/test/groovy/com/github/pbetkier/spockdemo/DemoSpockTest.groovy b/demo/spock-demo/src/test/groovy/com/github/pbetkier/spockdemo/DemoSpockTest.groovy new file mode 100644 index 0000000..5810d8a --- /dev/null +++ b/demo/spock-demo/src/test/groovy/com/github/pbetkier/spockdemo/DemoSpockTest.groovy @@ -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") + } + +} diff --git a/tool/bump-to-next-release.sh b/tool/bump-to-next-release.sh index 89b44b5..43a8698 100755 --- a/tool/bump-to-next-release.sh +++ b/tool/bump-to-next-release.sh @@ -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>/${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/${VERSION}<\/testable.version>/${NEXT}<\/testable.version>/" $pom done for md in docs/zh-cn/doc/setup.md docs/en-us/doc/setup.md; do