Versions Compared

Key

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

...

Code Block
languagegroovy
    def "NewListPricePack Element test - all data defined returns calculated value"() {
        when:

        TestRun testRun = TestRun.builder()
                .withLogicTestDoubles("out": [
                        "CurrentListPrice"             : 10,
                        "NewSOPricePack"               : 20,
                        "SOPriceActual"                : 2,
                        "IsResultElementNewListPrice":true
                ])
                .buildElementTest(LOGIC_DIR, ELEMENT_NAME)

        and:
        Script script = testRun.getElementScript()

        then:
        testRun.execute()
                .getElementTestResult() == 100
    }

How to mock api.global or api.local variables

This is necessary if element or function you test is using any value from api.global or api.local table. See this example, especially lines 8 and 12. This sample defines global table, api.local table is defined the same way - just change the keyword to “local”.

Code Block
   def "getRetailerSIRecords returns records as array of maps with values in big decimal"() {
        when:

        def countryArray = ["FRANCE", "ROMANIA"]
        def extTyreId = "515145"
        def startMonthExternalPricingSI = "201901"
        def endMonthExternalPricingSI = "202401"
        def globalTable = ["EU3CountryArray": [["attribute1": "FRANCE", "attribute11": "FRANCE",], ["attribute1": "ROMANIA", "attribute11": "ROMANIA"]]]

        TestRun testRun = TestRun.builder()
                .withLogicTestDoubles(["api": [
                        "global"                : globalTable,
                        "findLookupTable"       : { String name -> return ["id": "10"] },
                        "getMaxFindResultsLimit": { -> return 200 },
                        "find"                  : { String typecode, Integer start, Integer end, String sortBy, def fieldsList, def filter1, def filter2, def filter3, def filter4, def filter5 ->
                            return [
                                    ["key1": "FRANCE", "attribute10": "64.6"],
                                    ["key1": "FRANCE", "attribute10": "64.6"],
                                    ["key1": "FRANCE", "attribute10": "64.6"],
                                    ["key1": "FRANCE", "attribute10": "64.6"],
                                    ["key1": "FRANCE", "attribute10": "64.6"],
                                    ["key1": "ROMANIA", "attribute10": "63.4620732"],
                                    ["key1": "ROMANIA", "attribute10": "63.4620732"]
                            ]
                        }

                ], "libs"                   : getSharedLibrary()])
                .buildElementTest(LOGIC_DIR, ELEMENT_NAME)
        then:
        testRun.getElementScript().getRetailerSIRecords(countryArray, extTyreId, startMonthExternalPricingSI, endMonthExternalPricingSI) == [
                [Country: "FRANCE", RetailerSIPriceEUR: 64.6],
                [Country: "FRANCE", RetailerSIPriceEUR: 64.6],
                [Country: "FRANCE", RetailerSIPriceEUR: 64.6],
                [Country: "FRANCE", RetailerSIPriceEUR: 64.6],
                [Country: "FRANCE", RetailerSIPriceEUR: 64.6],
                [Country: "ROMANIA", RetailerSIPriceEUR: 63.4620732],
                [Country: "ROMANIA", RetailerSIPriceEUR: 63.4620732]]

    }

How to mock function from (shared) Library

Let’s say our function is using another function from shared library and we have to mock it. You can either mock it inside test or mock it as separate function. Here you can se how to do it. See the line 1-10 and 29. Keyword you need is “libs”

Code Block
    def getSharedLibrary(String val) {
        return [
                "Library": [
                        "Conversions": [
                                "convertToBigDecimal": { value -> return value?.toBigDecimal() }
                        ]
                ]
        ]
    }
    
        def "getRetailerSIRecords returns records as array of maps with values in big decimal"() {
        when:

        def countryArray = ["FRANCE", "ROMANIA"]
        def extTyreId = "515145"
        def startMonthExternalPricingSI = "201901"
        def endMonthExternalPricingSI = "202401"
        def globalTable = ["EU3CountryArray": [["attribute1": "FRANCE", "attribute11": "FRANCE",], ["attribute1": "ROMANIA", "attribute11": "ROMANIA"]]]

        TestRun testRun = TestRun.builder()
                .withLogicTestDoubles(["api": [
                        "global"                : globalTable,
                        "findLookupTable"       : { String name -> return ["id": "10"] },
                        "getMaxFindResultsLimit": { -> return 200 },
                        "find"                  : { String typecode, Integer start, Integer end, String sortBy, def fieldsList, def filter1, def filter2, def filter3, def filter4, def filter5 ->
                            return [["key1": "FRANCE", "attribute10": "64.6"]]
                        }

                ], "libs"                   : getSharedLibrary()])
                .buildElementTest(LOGIC_DIR, ELEMENT_NAME)
        then:
        testRun.getElementScript().getRetailerSIRecords(countryArray, extTyreId, startMonthExternalPricingSI, endMonthExternalPricingSI) == [
                [Country: "FRANCE", RetailerSIPriceEUR: 64.6]]

    }
    

...

Test Function with Local or Global table

Let’s have a element that modifies local / global table and always returns null. The questions is - how to test such element if return value is always the same (null)? Well, it is not that hard as you may think.

...

  • 5 - where input value of local table is defined.

  • 23 where we check if return value from elements is null

  • 24 - where we check if value in local map has been changed

...

Test Element or Function using function from another element

Let’s have element (simplified) using function from another element (line 3, element Library in the same logic).

...