New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@Spy @InjectMocks dependency A->spy(B)->C not injected correctly when using constructor dependency injection #1516
Labels
Comments
arnor2000
pushed a commit
to arnor2000/mockito
that referenced
this issue
May 9, 2019
…be injected into other @InjectMock
arnor2000
pushed a commit
to arnor2000/mockito
that referenced
this issue
May 9, 2019
Allows @SPY with @Injectmocks to be injected into other @Injectmocks
arnor2000
added a commit
to arnor2000/mockito
that referenced
this issue
May 10, 2019
Allows Spies with @Injectmocks to be injected into other @Injectmocks
arnor2000
added a commit
to arnor2000/mockito
that referenced
this issue
May 10, 2019
Allows Spies with @Injectmocks to be injected into other @Injectmocks
arnor2000
added a commit
to arnor2000/mockito
that referenced
this issue
May 10, 2019
Allows Spies with @Injectmocks to be injected into other @Injectmocks
Closed
8 tasks
arnor2000
added a commit
to arnor2000/mockito
that referenced
this issue
May 13, 2019
Allows Spies with @Injectmocks to be injected into other @Injectmocks
Open
8 tasks
arnor2000
added a commit
to arnor2000/mockito
that referenced
this issue
Mar 5, 2020
Allows Spies with @Injectmocks to be injected into other @Injectmocks
arnor2000
added a commit
to arnor2000/mockito
that referenced
this issue
Oct 12, 2021
…o be injected into other @Injectmocks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am trying to do a test on class A, with a dependency A->B to be spied, and a transitive dependency B->C
This code is typical in a spring application, when A is a high-level service, C is a low-level one (like a Repository), and B is simply a helper service class that delegate most calls to C
To use
@Spy
on instance of B, requires to mock C, and inject C into B...a typical test code should be
This generally works fine with standard spring injections for code like
... BUT fails when using Constructor dependency injection for fields !!!
This is absurd since constructor injection is precisely a best practice for enforcing code stability regarding test
There are many strange situations, while testing different combinations of annotations on real class (
@Inject
,@Autowired
..), and@Spy
,@InjectMocks
in test classEither
case 1/ spy NOT injected in a.b ... so causing NPE in spy execution
case 2/ spy NOT injected in b.c ... but injected in a.b ... so causing NPE in spy execution
case 3/ MockitoException Unable to initialize
@Spy
annotated field 'b', Please ensure that the type 'CtorB' has a no-arg constructorNotice also that the final desired scenario would be to use lombok generated full constructor.. but it also fails !!
I have joined a full maven test project that reproduce many of these scenarios
test-mockito.zip
You can also browse it (up-to-date) here.
The text was updated successfully, but these errors were encountered: