We have some problems figuring out how to mock complex data and operations on it.

First the problems with generating complex data:

In one of our components we have really complex data structures where the logic of our classes is executed. Unfortunate there are some bugs, where it is necessary to use a big data model to reproduce the case in a unit or integration test. The first idea was to write a script which converts the model to executable code, with which the data can be generated. To show the complexity of the models, these converted models had 20000++ line of code per model.

The problem with the first approach was, that we had some little mistakes in our script and every time we identified one we had to rewrite all models. That was no good idea. The second idea is to use the xml files in which they are saved. Before the test starts we use a class to convert the xml file to the model. The result is better than the first idea, but with this we will never have plain unit tests, because we always rely on the class used for the conversion.

Has someone a better idea how to deal with this issue?

The second problem is even harder to solve: manipulation of this complex data.

We have a integration test of another component and want to mock all its dependencies. Somehow we create the initial data used by the component (some kind of AST). Now we a syntax check component as a dependency. This component takes in the data and decorates the data. So in the end we have another manipulated AST in the test.

Here we have no idea, how to mock this behavior of the syntax check.

   > We use a class to convert the xml file to the model. ... 
   > But with this we will never have plain unit tests.

We use a similar approach. Since we already have json (de-)serialization we use this instead of xml. The sample data is included as a test-resource so we do not rely on filesystem-stream to use the data.

To further reduce the test-dependency on huge data modells in the modell we seperate model-navigation from modell-businesslogic-calculation. Example:

There is a static modell-businesslogic-calculation-method without navigation

Order.calculateTotal(IOrderItem[] items, ICustomer customer)

and a method that uses navigation-methods

Order.calculateTotal() {Order.calculateTotal(this.getItems(), this.getCustomer())}

To test order-businesslogic-calculation we do not need to mock an Order. We use the internal static order-businesslogic-calculation-method that has no dependencies to the Order-OrderItems and Order-Customer.

To test Manipulation of this complex data have a look at approvaltests. approvaltests actually record test-output and fail if the test-output has changed. Then a human has to approve that the calculation was ok. The next time the approved test is rexecuted the test will not fail any more (until output changes again)

  • We don't need to test the manipulation of the data. We need to simulate it for the test. – Yggdrasil Jul 18 '13 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.