Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Current »

You can also test individual functions you have created in your logic or in the groovy library.

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

import java.math.RoundingMode

BigDecimal round(BigDecimal number, int decimalPlaces) {
    if (number == null) {
        return null
    }
    return number.setScale(decimalPlaces, RoundingMode.ROUND_HALF_UP)
}

Create a unit test from in logic editor in Studio to get the template.

When testing method calls, delete the condition for evaluation of the element result:

testRun.execute().getElementTestResult() != null

In our example we want to evaluate this condition instead:

script.round(10.449, 0) == 10

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

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

package MyLibrary

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

class RoundingUtlsTest extends Specification {

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


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

Run the test using the test icon next to the test name.

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:

package MyLibrary

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

class RoundingUtlsTest extends Specification {

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


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

Run the test using the test icon next to the test name.

  • No labels