Export to GitHub

mockito - issue #421

Mockito does not pick the "biggest" constructor for injection, but the "default" constructor.


Posted on Feb 16, 2013 by Happy Lion

Given the fact that there is not a single "biggest" constructor, but two or more with same length, and given the fact that the first-in-topdown-sequence has at-least-one not-mockable argument, mockito actually does not pick that constructor, but it picks the default constructor.

Scenario:

public A { public A(int i); public A(MockableInterface i); }

Expected result: As both constructors have same argument count, one would expect the biggest-useable one (the second one) to be picked.

Actual result: Mockito fails as there is no default constructor.

From the user side, this feels like a bug, as the default constructor is not the "biggest" constructor. A(MockableInterface) has more arguments than the (missing) constructor, hence must be chosen.

Even if the mockito authors think that this behaviour is OK, it at least should be told clearly in the @InjectMocks JavaDocs, which says that the "biggest" constructor is choosen -- but not what will happen if there is no single one, and it also does not say that in this case the default -which is NOT the biggest- is chosen.

Comment #1

Posted on Apr 29, 2013 by Happy Horse

Good point. Thx for reporting. I will update the javadoc.

Comment #2

Posted on Apr 29, 2013 by Happy Horse

Also if you feel like it you can propose a PR to tweak the existing behavior to choose a matching constructor if this is not ambiguous.

Comment #3

Posted on Dec 23, 2013 by Swift Rabbit

Pull request

https://github.com/mockito/mockito/pull/29

Comment #4

Posted on Dec 24, 2013 by Happy Horse

Merged. Thx Marcin !

Comment #5

Posted on Aug 16, 2014 by Massive Monkey

(No comment was entered for this change.)

Comment #6

Posted on Aug 24, 2014 by Massive Monkey

(No comment was entered for this change.)

Comment #7

Posted on Aug 24, 2014 by Massive Monkey

(No comment was entered for this change.)

Status: Fixed

Labels:
Type-Defect Priority-Medium Javadoc Milestone-Release1.10.0