Closed GoogleCodeExporter closed 8 years ago
Hi Tom
There's nothing wrong here with the injection. You are initializing your mocks
two times. One with the runner and one with MockitoAnnotations.
Just use only one.
For information here's what happens :
Case A : non final field
1. MockitoJUnitRunner creates the mock "myDep", and the instance myClass using
constructor param "myDep"
2. MockitoAnnotations.init() creates a new mock "myDep", and sees that myClass
is already instanciated, so it tries setter injection AND field injection, the
new mock instance is injected.
3. The mock instance is the same in myDep field in test class and in myDep
field in production class, so when stubbing you are talking to the correct mock
instance.
Case B : final field
1. MockitoJUnitRunner creates the mock "myDep", and the instance myClass using
constructor param "myDep"
2. MockitoAnnotations.init() creates a new mock "myDep", and sees that myClass
is already instanciated, so it tries setter injection and field injection, BUT
field is final, the new mock instance is NOT injected.
3. The mock instance is NOT the same in myDep field in test class and in myDep
field in production class, so when stubbing you are talking to the mock
instance in test class, not the instance in the production class.
Original comment by brice.du...@gmail.com
on 13 Jan 2012 at 12:09
Ah right. I see. If I remove one of them, my Camel ProducerTemplate is not
getting injected into @Produce. This is probably a separate problem though.
I'll investigate.
Original comment by tommytastic
on 17 Jan 2012 at 1:59
Original comment by brice.du...@gmail.com
on 3 Sep 2012 at 10:00
Original issue reported on code.google.com by
tommytastic
on 13 Jan 2012 at 10:55