Unit Tests

Some processors implement custom logic that can be unit tested outside of a flow, e.g., the map mappings.

Unit tests leverage the SDK’s input() function to create a flow message object with auto-generated values for ownerId, messageId, etc. For example, the statement input(mapOf("value" to "testData")) would result in an object similar to the following:

{
  "flowId": "e6f66ba5-816f-4ae2-946b-e7452c2f82f0",
  "messageId": "ad531fab-3676-49b8-ba31-ff83774e9b61",
  "originalMessageId": "bd92cf06-71cd-4653-805f-7b923f67a86d",
  "ownerId": "292c987d-3960-4f88-ad44-8adccb14ff49",
  "payload": {
    "value": "testData"
  }
}

Stashes and message exchange properties can also be added via a callback function block. For example:

val input = input(...) {
    exchangeProperty("key", "value")
    stash("key", "value")
}

The generated object can then be used in either a mapping or script test context.

Mappings

Unit tests for mappings use the withMap() function to create a test context where mapping logic is executed against a flow message object. For example:

@Test
fun `GIVEN input THEN data is mapped correctly`() {
    val mappingConfig = mapConfig {
        mapSpec = myMapping
    }

    withMap(mappingConfig) {
        val input = input(...)
        val mappingResult = map(input)

        assertThat(mappingResult).isEqualTo(...)
    }
}

Mappings are written as strings, so the mapping must be converted to a MapConfig object first via the mapConfig builder before it is passed to the withMap() function. The map() function runs the mapping logic from the current context (i.e., the MapConfig object’s mapSpec) and returns the results. Assertions are then written against those results.

Keep in mind that JSON compliant objects and strings are both valid inputs for a mapping. The mapping won’t automatically assume that a JSON string should be parsed as an object. In an actual flow, a JSON string payload can be converted into an object with an inboundTransformationStrategy. When unit testing, however, it is up to you to either recreate the JSON compliant payload with Kotlin’s mapOf() function or use a serialization library to parse a JSON string.

See the tutorial on data mappings for a complete example of unit testing a mapping.