podam icon indicating copy to clipboard operation
podam copied to clipboard

To the question of finding the best constructor for POJO

Open daivanov opened this issue 13 years ago • 2 comments

Podam unit tests behave differently in JDK6 and JDK7. In JDK6 for BigInteger creation BigInteger(int numBits, Random rnd) is picked up, however, in JDK7 BigInteger(int bitLength, int certainty, Random rnd) is found first. The latter is really heavy call causing unit tests to do a heavy work for many minutes.

Trying to deal with this problem, I introduced a sorting for POJO constructors, in such a way, that constructors with Podam annotations will be tried first and then, if not successful, constructors with less parameters will be used. The rationale behind the change is so that in general mocking less parameters should be easier https://github.com/daivanov/podam/commit/260dc62c1c742c8ebdd3005c262ab33bb22b2fa7

However, I do understand that constructors with less parameters do not guarantee success, as meaning of parameters is not known to Podam.

Another point is that sorting will make Podam behave more predictable with different JDK versions.

daivanov avatar Nov 09 '13 13:11 daivanov

The point of a constructor is that, after it runs, it should set the correct state for a class. If the code has been written properly, with constructor chaining, it doesn't matter which constructor is invoked. The final class state should be correct according to its writer's intentions. Therefore I think that you could go ahead with the change (or have you already done it)?

mtedone avatar Nov 09 '13 17:11 mtedone

I've made a pull request https://github.com/mtedone/podam/pull/22

daivanov avatar Nov 10 '13 12:11 daivanov