Search code examples
grailsjunitspock

Spock - approximate comparisions


I’ve been looking for the Spock equivalent of the following convenience method in JUnit whereby you can do “approximate” comparisons. Does anyone know if such a thing exists?

/**
 * Asserts that two doubles or floats are equal to within a positive delta.
 */
assertEquals(double expected, double actual, double delta) 

Solution

  • There is build in function for that, described in official docs:

    when:
    def x = computeValue()
    
    then:
    expect x, closeTo(42, 0.01)
    

    Check the specs.