Versions Compared

Key

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

TestDoubles are objects that we pass to the test run that interact with the code under test. , i.e. All the all injected variables (api, quoteProcessor, rebateManager, ...).

The test double can be a fake or a mock. By default, when no test doubles are provided, some default fakes are injected to the elements to allow to run your the code execution. However, the test doubles needs need to be configured per test.

I

...

Warned You!

Generally, it is very good idea to avoid any need for this. Whenever a test double is needed to provide some dependency, please, check twice (or more) if the code can be rather changed to pass the dependency value as a parameter to some new function and then you can it is strongly recommended that you change the code so that it passes the dependency value to a new function as a parameter and then you test just this new function which will be a way . This way it is much easier to do test and maintain in future.

Map

...

Based Fakes

By default, we use fake objects created as maps. These default fake objects are defined in the upstream tdd4c library. In a test, one you can override properties on these fakes to provide the test specific behavior.

Paste code macro
languagegroovy
TestRun testRun = TestRun.builder()
        .withElementResult("NotComputedElement", "HardCodedValue")
        .withLogicTestDouble("api", [
        find: { typeCode -> [[attribute1: "a"],[attribute1: "b"],[attribute1: "c"]] }
])
        .buildLogicTest("MyLogic3")

This code will ensure , that the default fake fake for api API variable has overridden the find function that will be called in a test. Whenever MyLogic3 is executed, it tooks takes the default map based fake and adds the behavior specified in .withLogicTestDouble. This way one you can create test doubles with minimal effort.

There is also the .withElementTestDouble method. It specifies the overrides for the default + logic test double to be provided in a single element.

The methods api.getElement and api.getBinding is are handled gracefully in TestRun , and should always provide always the expected result. There should not be any need to implement it manually.

Object

...

Based Fakes

If map based fakes can not cannot be used one , you can provide Object object based implementation. This should be needed only when there is a collision of some groovy Groovy default method (groupBy() e.g. groupBy()) with the defined test double. No merging of the element test double with the logic test doubles will happen with the object based fakes.

Mocks

There are a few pfx PFX interfaces bundled with the tdd4c library. PublishedApi, DatamartContext and TableContext to name some of them. One You can use mockito the Mockito library to create a mocks and pass them as the test double. No merging or of the element test double with the logic test double will happen in this case.

...