Spock is a versatile unit testing framework.
Last time, I shared the basic functionality of the Spock framework, and based on that, I experimented with Groovy’s encapsulation methods, data-driven, and some of Groovy’s advanced syntax. There are still a few problems. I don’t know if it’s because I’m a Java/Groovy hybrid project, but I have a solution.
Share code for your reference:
package com.FunTester.spock.pratice import com.fun.config.PropertyUtils import com.fun.frame.SourceCode import org.slf4j.Logger import spock.lang.Shared import spock.lang.Specification import static com.fun.config.Constant.EMPTY import static com.fun.config.Constant.getLongFile import static com.fun.frame.Output.output import static com.fun.frame.SourceCode.* class Test02 extends Specification { @Shared def properties = PropertyUtils.getLocalProperties(getLongFile("1")) @Shared def cc = Arrays.asList(properties.getArrays("c")).stream().map {x -> Integer.valueOf(x)}.collect() as List @Shared def bb = Arrays.asList(properties.getArrays("b")).stream().map {x -> Integer.valueOf(x)}.collect() as List @Shared def aa = Arrays.asList(properties.getArrays("a")).stream().map {x -> Integer.valueOf(x)}.collect() as List @Shared Logger logger = getLogger (Test02. Class. GetName ()) def setup () {logger. The info (" went test method ")} def cleanup () {logger. The info (" testing method is over ")} def SetupSpec () {logger.info(" Test class [${getClass().getName()}] started ")} def cleanupSpec() {setupSpec() {logger.info(" test class [${getClass().getName()}] started ")} def cleanupSpec() { Logger. info(" test class [${getClass().getName()}] end ")} def "test data driver Demo"() {given: int c = 0 expect: 10 == a + b + C where: A | b | 1 | 9 8, no. 2} def "test data read and write data driven" () {given: def a = 0 def arrays = properties.getArrays("id") def s1 = properties.getPropertyInt("size1") def s2 = properties.getPropertyInt("size2") def list = Arrays.asList(arrays).stream().filter {x -> x.length() > 1}.collect() as List expect: = array.size () s2 == list.size()} def "Test custom objects "() {given: Def per = new per () per.age = 23 per.name = "FunTester" def a = per expect: A.name == "FunTester"} def "thread safety test "() {given: Range (2).foreach {new Per().start()} sleep(1000) output(per.i) expect: 4 == per. I} def ""() {given:" "expect: c * c == a * a + b * b where: C << cc b << bb a << aa} def "test array 0.. "() {expect: true == sourcecode. isNumber(x + EMPTY) where: "x << (0.. If (x -> x % 2 == 1) {if (x -> x % 2 == 1) {if (x -> x % 2 == 1) {if (x -> x % 2 == 1) {if (x -> x % 2 == 1)}. Collect.size () == 5 collect.contains(3) collect.contains(5)} /** * extends Thread {static int I @Override public void run() { i++ sleep(100) i++ } Per() { } Per(String name, int age) { this() this.name = name this.age = age } String name int age static def getAll() { i } } }Copy the code
Next time I’ll do some spock code demonstrations for my own utility classes and wrapped request objects.
Technical articles selected
- Java one line of code to print a heart
- Linux performance monitoring software Netdata Chinese version
- Interface Test Code Coverage (JACOCO) solution sharing
- Performance testing framework
- How to perform performance tests on a Linux command line interface
- Graphic HTTP brain map
- Automatically turn the Swagger document into test code
- Five lines of code to build a static blog
- Probe into the Linear interface testing framework based on Java
- Mid-practice for Selenium testing in JUnit
Selected non-technical articles
- Why software testing as a career path?
- Programming thinking for everyone
- 7 Steps to becoming a Good Automation Test Engineer
- An important reason for the existence of manual testing
- 7 Skills to become an automated test
- Automated and manual testing to keep the balance!
- Automated test lifecycle
- How do YOU introduce automated testing in DevOps