New issue 289 by m...@daniel-spilker.com: Mock not injected into
superclasses
http://code.google.com/p/mockito/issues/detail?id=289
What steps will reproduce the problem?
SomeService.java:
public class SomeService {
public void doSomething() {
}
}
AbstractSystemUnderTest.java:
import javax.inject.Inject;
public class AbstractSystemUnderTest {
@Inject
private SomeService someService;
public void doSomething() {
someService.doSomething();
}
}
import javax.inject.Inject;
SystemUnderTest.java:
public class SystemUnderTest extends AbstractSystemUnderTest {
@Inject
private SomeService someService;
public void doSomethingElse() {
someService.doSomething();
}
}
TheTest.java:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class TheTest {
@InjectMocks
private SystemUnderTest systemUnderTest;
@Mock
private SomeService someService;
@Test
public void someTest() {
systemUnderTest.doSomething();
}
}
What is the expected output? What do you see instead?
I would expect the test to succeed, but instead a NullPointerException is
thrown:
java.lang.NullPointerException
at AbstractSystemUnderTest.doSomething(AbstractSystemUnderTest.java:8)
at TheTest.someTest(TheTest.java:17)
...
What version of the product are you using? On what operating system?
I am using mockito-core 1.9.0-rc1
Please provide any additional information below.
The problem occurs only when a mock has to be injected in the class itself
and on one of its super classes. The injection algorithm seems to stop when
it found one field to inject for a given mock, but instead it should
continue to find matching fields in all super classes.
Comment #1 on issue 289 by brice.du...@gmail.com: Mock not injected into
superclasses
http://code.google.com/p/mockito/issues/detail?id=289
This case wasn't on the roadmap, when coding this small injection utility.
This feature was never itnended to be a full featured injection framework.
Beside I think it's rather a wrong to have the very same field of the
parent class in a sub-type. You should really name it differently. Or maybe
event change the design with composition in mind instead of inheritance.
Thanks for the report, Daniel
Is this a regression VS 1.8.5? E.g. was this test working for you with
Mockito 1.8.5?
As Brice says - the injection stuff in Mockito is simple - we're a mocking
framework and not a Guice =) If you need this feature please consider
contributing.
Cheers :)
I just checkt and this is a regression vs. 1.8.5. indeed.
I like the simplicy of Mockito, no need to become a Guice or a Spring.
Before using @InjectMocks we had our own injection helper, which suffered
from the same problem. I will look into this and see if I can provide a
patch.
Oh, damn! I remember now, there's a piece of code that was added to avoid
injecting the dependency several times. This code was added during the
fixing for issue 236 because mockito was a bit too agressive at injecting
mocks.
Fixing this issue might require a clever way to inject things here and
there. No that this is bad thing, but Mockito tries to figure out by itself
types and fields at best and without configuration; it has no knowledge of
CDI/Spring/Guice annotations; and this choice has also some drawbacks.
Note however this part of the code will need revamp anyway to provide
configurable resolving mechanism.
I think we should fix this regression before releasing 1.9.0. Thoughts?
I created a simple patch. See the attachment. This patch simply considers
the whole set of mocks for injection into super classes. Currently only
mocks which have not already been injected are considered for super classes.
Attachments:
patch.txt 1.5 KB
Cool, you beat me there ;)
However if we do that, it means mocks can be injected more than once in the
instance then, but only once for a type in the hierarchy. I hope it won't
raise more issues.
In my opinion I think it is still weird to have a dependency injected two
times in a field with the same name.
Comment #8 on issue 289 by szcze...@gmail.com: Mock not injected into
superclasses
http://code.google.com/p/mockito/issues/detail?id=289
(No comment was entered for this change.)
Fixed in changeset f6aa67247b92
Fixed in revision f6aa67247b92