Closed GoogleCodeExporter closed 9 years ago
Hi,
There is no problem with the way you are using Mockito, final static fields
should not be targetted for injection i'll fix it.
However, you should mind the Demeter law, instead of that :
objectWithList.getLogs().add("logic completed");
You should write :
objectWithList.addLog("logic completed");
So it shall be a better solution, and it shall work for your problem too (with
InjectMocks).
Hope that helps.
Original comment by brice.du...@gmail.com
on 18 May 2011 at 9:27
Thank you for your quick feedback. I appreciate it!
Original comment by dodozhan...@gmail.com
on 18 May 2011 at 9:50
You are welcome :)
Fixed in
http://code.google.com/p/mockito/source/detail?r=a91c45ecabf30253fa12dfd3176451c
867bf2b9d
Original comment by brice.du...@gmail.com
on 18 May 2011 at 11:37
Original comment by szcze...@gmail.com
on 3 Jul 2011 at 12:43
Original comment by brice.du...@gmail.com
on 3 Sep 2012 at 10:00
Hey guys, why have you disabled injecting final fields ? I understand why
static finals are disabled, but finals should be (in my opinion) injectable.
Original comment by pio...@gmail.com
on 26 Oct 2012 at 10:37
Because InjectMocks should respect object privacy, as Mockito injects
automatically, if final fields are candidate to injection it can cause unwanted
injection for too many people, so we had to make this injection behavior less
agressive in this regard.
Maybe you can use the following workaround, you can design your object to be
configurable just as SpringSource guys are designing some of their objects. For
example one could write.
class TheObject {
private final SomeStrategy DEFAULT_STRATEGY = new DefaultStrategy();
private SomeStrategy strategy = DEFAULT_STRATEGY;
// using a setter, but this might as well be injected via a constructor, which is even more respectfull of the OO pradigm
public void setStrategy(SomeStrategy) { this.strategy = strategy; }
}
This has the benefit to make your object configurable and then ready for reuse.
Hope that helps
Brice
Original comment by brice.du...@gmail.com
on 26 Oct 2012 at 10:56
It's true that it can cause unwanted injections, but why cannot this be a
default behaviour with possibility of changing injection strategy to allow
final injections ?
Original comment by pio...@gmail.com
on 26 Oct 2012 at 1:35
Injecting won't be made default again, this was a bogus injection, plus we
won't force 80% users to modify their tests to disable final injection.
However we could add an option to enable final injection. However we might
avoid that and instead propose users to provide their own custom strategy.
Original comment by brice.du...@gmail.com
on 26 Oct 2012 at 2:15
You got me wrong. I agree that injecting finals should be disabled by default.
Customizable injection strategy would be nice :-) Thanks for your replies.
Original comment by pio...@gmail.com
on 26 Oct 2012 at 2:24
Oh ok :)
You are welcome, thank you for the feedback too :)
Original comment by brice.du...@gmail.com
on 26 Oct 2012 at 2:28
Original issue reported on code.google.com by
dodozhan...@gmail.com
on 17 May 2011 at 11:24