Skip to content

katoquro/spock-toolbox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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))

About

No description or website provided.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published