...
This section contains the following sample test definitions:
Table of Contents |
---|
...
|
...
|
...
|
How to Mock API Function (findLookupTable or find etc.)
You can do it via the withLogicTestDouble
function or as part of the withLogicTestDoubles
function. See line 13 in this sample. The withLogicTestDoubles
function simply contains an “api” map, with mocked API functions.
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 api.stream
Simply define api.stream
the same way as any other API function and add .stream()
at the end of the values list. For a sample see line 26.
Code Block | ||
---|---|---|
| ||
def "getPXasMap return map with specified attribute as key"() {
when:
TestRun testRun = TestRun.builder()
.withLogicTestDouble("api", [
"getMaxFindResultsLimit": { -> return 200 },
"stream" : { String typeCode, def orderBy, def fields, def filters ->
return [
[
"attribute18": "*",
"attribute17": "*",
"attribute39": "*",
"attribute37": "*"
],
[
"attribute18": "SNOW",
"attribute17": "RIKEN",
"attribute39": "PC",
"attribute37": "Winter"
],
[
"attribute18": "ENERGY SAVER",
"attribute17": "CompanyABC",
"attribute39": "PC",
"attribute37": "Summer"
]
].stream()
}
])
.buildElementTest LOGIC_DIR, ELEMENT_NAME
and:
Script script = testRun.getElementScript()
then:
script.getPXasMap("CommercialCatalog", "attribute18", ["attribute18", "attribute17", "attribute37", "attribute39"]) == ["*" : ["attribute18": "*", "attribute17": "*", "attribute39": "*", "attribute37": "*"],
"SNOW" : ["attribute18": "SNOW", "attribute17": "RIKEN", "attribute39": "PC", "attribute37": "Winter"],
"ENERGY SAVER": ["attribute18": "ENERGY SAVER", "attribute17": "CompanyABC", "attribute39": "PC", "attribute37": "Summer"]]
} |
How to Mock Another Element Result
This is a very common case – elements or functions use another element result and you have to mock it in your test. You can do it with an “out” map like in this example, see line 5 and below.
Code Block | ||
---|---|---|
| ||
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 an element or function you test uses a value from the api.global or api.local table. See this example, especially lines 8 and 12. This sample defines a global table; the 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 uses another function from SharedLibrary and we have to mock it. You can either mock it inside the test or mock it as a separate function. Here you can see how to do it: lines 1-10 and 29. The 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]]
}
|
How to Test Function if It Modifies Local or Global Table and Always Returns null
Let’s have an element that modifies the local/global table and always returns null. The questions is: how to test such an element if the return value is always the same (null)?
Let’s have the element code that just adds a value to a local map. The most important lines are 26 where a map value is added and 29 where null is always returned.
Code Block |
---|
if (out.EU3LPG) {
for (eu3CountryRecord in api.global.EU3CountryArray) {
def highLowRMLogic = null
def EU3countryName = eu3CountryRecord.attribute1
def eu3countryRecord = api.local.countryMatrixMap.get(EU3countryName)
def targetSIPriceLowRM = eu3countryRecord.get("TargetSIPriceLowRM")
def targetSIPriceHighRM = eu3countryRecord.get("TargetSIPriceHighRM")
def targetSIPriceHighMaxRM = eu3countryRecord.get("TargetSIPriceHighMaxRM")
def targetSIPriceHighMinRM = eu3countryRecord.get("TargetSIPriceHighMinRM")
if (targetSIPriceLowRM == null && targetSIPriceHighRM == null) {
highLowRMLogic = null
} else if (targetSIPriceLowRM == null) {
highLowRMLogic = "Adjusted to High Comp Index Target"
} else if (targetSIPriceHighMaxRM != null && targetSIPriceHighMinRM != null) {
if (targetSIPriceHighMaxRM <= targetSIPriceLowRM && targetSIPriceLowRM <= targetSIPriceHighMinRM) {
highLowRMLogic = "Adjusted to Low Comp Index Target"
} else if (targetSIPriceLowRM > targetSIPriceHighMinRM) {
highLowRMLogic = "Adjusted to High Min Comp Index Target"
} else if (targetSIPriceLowRM < targetSIPriceHighMinRM) {
highLowRMLogic = "Adjusted to High Max Comp Index Target"
}
}
api.local.countryMatrixMap.get(EU3countryName).put("HighLowRMLogic", highLowRMLogic)
}
}
return null |
And now, the test for such an element can look like this:
Code Block |
---|
def "HighLowRMLogic value stored in local map - TargetSIPriceLowRM is null "() {
when:
def globalTable = ["EU3CountryArray": [["attribute1": "FRANCE", "attribute11": "FRANCE",], ["attribute1": "ROMANIA", "attribute11": "ROMANIA"]]]
def localTable = ["countryMatrixMap": ["FRANCE" : ["TargetSIPriceLowRM": null, "TargetSIPriceHighRM": 10],
"ROMANIA": [:]]]
TestRun testRun = TestRun.builder()
.withLogicTestDoubles(
"out": [
"EU3LPG": true
],
"api":
["global": globalTable,
"local" : localTable]
)
.buildElementTest(LOGIC_DIR, ELEMENT_NAME)
and:
Script script = testRun.getElementScript()
then:
testRun.execute().getElementTestResult() == null
localTable == ["countryMatrixMap": ["FRANCE": ["TargetSIPriceLowRM": null, "TargetSIPriceHighRM": 10, "HighLowRMLogic": "Adjusted to High Comp Index Target"],
"ROMANIA":["HighLowRMLogic":null]]] |
The most important lines are:
5 where the input value of the local table is defined.
23 where we check if the return value from elements is null.
24 where we check if the value in the local map has been changed.
How to Test Element/Function Using Function from Another Element
Let’s have a (simplified) element using a function from another element (see line 3, element Library in the same logic).
Code Block |
---|
if ((out.PricingStrategyProposed || adjustmentFactorVs == "current") && out.IsResultElementNewListPrice) {
def newListPrice = (listPrice * (1 + adjustmentFactorPct)) + adjustmentFactorUnit
def newListPriceAfterThresholdsRounded = Library.applyThresholdOnResultPriceWithRounding(newListPrice, out.BaseListPrice, out.IncreaseThresholdPct, out.DecreaseThresholdPct)
return newListPriceAfterThresholdsRounded
} |
This is possible if you have Groovy in version 2.4.12 and lower, but not possible if you have a higher version. You cannot mock the function via withLogicTestDoubles
and the function withAdditionalElementInitialized
does not work properly. See the related issue for a test sample and result.