spock-toolbox

Toolbox is a set of small utilities to make Spock Framework Specifications more readable and easier to write

License

License

Categories

Categories

Spock Application Testing & Monitoring
GroupId

GroupId

com.ainrif.gears
ArtifactId

ArtifactId

spock-toolbox
Last Version

Last Version

0.3.0
Release Date

Release Date

Type

Type

jar
Description

Description

spock-toolbox
Toolbox is a set of small utilities to make Spock Framework Specifications more readable and easier to write
Project URL

Project URL

https://github.com/ainrif/spock-toolbox/
Project Organization

Project Organization

Ainrif
Source Code Management

Source Code Management

https://github.com/ainrif/spock-toolbox

Download spock-toolbox

How to add to project

<!-- https://jarcasting.com/artifacts/com.ainrif.gears/spock-toolbox/ -->
<dependency>
    <groupId>com.ainrif.gears</groupId>
    <artifactId>spock-toolbox</artifactId>
    <version>0.3.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.ainrif.gears/spock-toolbox/
implementation 'com.ainrif.gears:spock-toolbox:0.3.0'
// https://jarcasting.com/artifacts/com.ainrif.gears/spock-toolbox/
implementation ("com.ainrif.gears:spock-toolbox:0.3.0")
'com.ainrif.gears:spock-toolbox:jar:0.3.0'
<dependency org="com.ainrif.gears" name="spock-toolbox" rev="0.3.0">
  <artifact name="spock-toolbox" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.ainrif.gears', module='spock-toolbox', version='0.3.0')
)
libraryDependencies += "com.ainrif.gears" % "spock-toolbox" % "0.3.0"
[com.ainrif.gears/spock-toolbox "0.3.0"]

Dependencies

compile (6)

Group / Artifact Type Version
org.codehaus.groovy : groovy-all jar 2.4.13
org.slf4j : slf4j-api jar 1.7.25
org.spockframework : spock-core jar 1.0-groovy-2.4
org.hamcrest : hamcrest-all jar 1.3
junit : junit jar 4.12
org.unitils : unitils-core jar 3.4.6

Project Modules

There are no modules declared in this project.

Spock Toolbox

Toolbox is a set of small utilities to make Spock Framework Specifications more readable and easier to write

Getting Started

Get artifact from Maven Central Repo

  • group: com.ainrif.gears
  • artifact: spock-toolbox
  • version: Version

Usage

There are several "tools" which are accessible through the one entry point com.ainrif.gears.spock_toolbox.SpockToolbox

Replicator

Replicator creates objects based on given settings and check that there are no extra atoms setters which weren't used

def pogo = SpockToolbox.replicate(CustomPogoClass) {
    pogoStringField = 'value'
    pogoIntField = 42
}

assert pogo.pogoStringField == 'value'
assert pogo.pogoIntField == 42

// throws exception about there are fields (maybe after new feature or refactoring)
// which were not set throug the initialization
def pogo = SpockToolbox.replicate(CustomPogoClass) {
    pogoStringField = 'value'
}

Tricorder

Tricorder analyzes given objects

Reflection equals

To compare two objects toolbox provides reflects method. You can use it in assertion stanza.

// Spock Specification on Groovy  
then:
SpockToolbox.reflects(actual, expected)

// Java assertion
assert SpockToolbox.reflects(actual, expected).asBoolean()

Exclude fields from comparison

Sometimes objects have dynamic fields (like time, date, id) and should be excluded from comparison.

SpockToolbox.reflects(actual, expected)
        .excludeField('fieldToxclude')
        .excludeField('nestedObject.fieldToExclude')

Exclude map keys and array items from comparison

There is special syntax to exclude map keys and array items by index

SpockToolbox.reflects(actual, expected)
        .excludeField('mapField.keyToExclude')
        .excludeField('arrayField.1')
        .excludeField('arrayField.2')
        .excludeField('arrayField') // to exclude the whole array

If some fields are undefined are dynamic they can be replaced with wildcard *

SpockToolbox.reflects(actual, expected)
        .excludeField('a.*.c') // a.b.c, a.d.c etc. will be excluded
        .excludeField('a.b.*') // the same as to exclude a.b

Custom comparator for objects

Some rules of comparison can be override with custom comparators. Custom comparators must implement org.unitils.reflectionassert.comparator.Comparator and set into the reflection builder:

SpockToolbox.reflects(actual, expected)
        .comparator(new CustomComparator(settings))

Predefined comparators (a.k.a comparison modes)

Toolbox already has some predefined comparators which are singletons and can be used via mode method. Modes usually are immutable and must have default constructor. Every customization produces new Comparator which can be used to override the previous mode.

Modes can be found at package: com.ainrif.gears.spock_toolbox.comparator

  1. DOUBLE_SCALE - relaxed comparison for doubles with given scale (default is 1e-14)
  2. IGNORE_DEFAULTS - relaxed comparison for null and default values of primitive types
  3. IGNORE_TIME_DIFF - objects with diff only in time are equal, also supports jsr310
  4. STRICT_ORDER - validated orders for ordered collections
  5. IGNORE_ABSENT_EXPECTED_FIELDS - if expected is superclass of actual absent fields won't generate difference
SpockToolbox.reflects(actual, expected)
        .mode(STRICT_ORDER)
        .comparator(DOUBLE_SCALE.scale(1e-2d))

Versions

Version
0.3.0