joepadde / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

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

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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.

Original issue reported on code.google.com by headcrashing on 16 Feb 2013 at 2:24

GoogleCodeExporter commented 8 years ago
Good point. Thx for reporting. I will update the javadoc.

Original comment by brice.du...@gmail.com on 29 Apr 2013 at 4:00

GoogleCodeExporter commented 8 years ago
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.

Original comment by brice.du...@gmail.com on 29 Apr 2013 at 4:02

GoogleCodeExporter commented 8 years ago
Pull request

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

Original comment by marcin.g...@gmail.com on 23 Dec 2013 at 10:18

GoogleCodeExporter commented 8 years ago
Merged. Thx Marcin !

Original comment by brice.du...@gmail.com on 24 Dec 2013 at 11:39

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 16 Aug 2014 at 2:43

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 24 Aug 2014 at 3:14

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 24 Aug 2014 at 3:50