Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Assume there is a library logicthat has RoundingUtils element with a method that rounds the value to the desired count of decimal places:

...

If your method from your unit test example does not call any Pricefx API, you can always remove the line that mocks of fakes the api variable:

.withLogicTestDouble("api", [:]).

Code Block
languagegroovy
package MyLibrary

import net.pricefx.tdd4c.TestRun
import spock.lang.Specification

class RoundingUtlsTestRoundingUtilsTest extends Specification {

    def LOGIC_DIR = "MyLibrary+2020-01-01"
    def ELEMENT_NAME = "RoundingUtlsRoundingUtils"


    def "base rounding"() {
        when:
        TestRun testRun = TestRun.builder()
                .buildElementTest(LOGIC_DIR, ELEMENT_NAME)

        and:
        Script script = testRun.getElementScript()

        then:
        script.round(10.449, 0) == 10
    }
}

...

Now we want to repeat the same unit test but for multiple inputs. Spock offers a nice feature Data tables that allows to put input and expected output values in a text table:

Code Block
languagegroovy
package MyLibrary

import net.pricefx.tdd4c.TestRun
import spock.lang.Specification

class RoundingUtlsTestRoundingUtilsTest extends Specification {

    def LOGIC_DIR = "MyLibrary+2020-01-01"
    def ELEMENT_NAME = "RoundingUtlsRoundingUtils"


    def "base rounding"() {
        when:
        TestRun testRun = TestRun.builder()
                .buildElementTest(LOGIC_DIR, ELEMENT_NAME)

        and:
        Script script = testRun.getElementScript()

        then:
        script.round(price, dp) == rounded

        where:
        price    | dp || rounded
        10.449   | 0  || 10
        10.678   | 0  || 11
        2.15     | 0  || 2
        2.95     | 0  || 3
        3.00     | 0  || 3
        0        | 0  || 0
        null     | 0  || null
        10.47449 | 3  || 10.474
        10.47678 | 3  || 10.477
        2.125    | 3  || 2.125
        3.000    | 3  || 3.000
        0        | 3  || 0
        null     | 3  || null
    }
}

...