Unit testing mathematical code

Tags: , , ,



I am writing a small utility for calculating a complicated mathematical formula (using commons-math library for integration and root finding). I was trying to write it in the same way as a normal business application, however I found that I am getting a quickly increasing number of classes. To get the first step of the calculations (1 line formula with 2 integrals), I already wrote 3 classes for every tiny bit of the calculation, so that I can use dependency injection and properly mock all the calls to commons-math. It is kind of getting out of control though, I will end up with 20 classes for a problem that could be solved on 2 screens in one class (without unit testing). What would be your preferred approach? I am very tempted to only rely on acceptance and higher level tests for this one.

Answer

Don’t let testing create a completely unusable and incomprehensible code. And don’t overengineer functional being with object-oriented approach.

You’re testing a function, i.e. stateless being that produces the same result for the same arguments. And I think that’s how you should test it: Give it arguments from all possible equivalence classes and assert on the result.



Source: stackoverflow