"Argument should be a mock, but is" error when combine @InjectMocks with @Spy
See original GitHub issueIn mockito <=2.0.12 there was possible to use @InjectMocks on spy object, but seems like this was changed since 2.0.13-beta version:
Argument should be a mock, but is: class mockito.bug.snippet.ArgumentShouldBeAMockButIsTest$B
Test case:
package mockito.bug.snippet;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
public class ArgumentShouldBeAMockButIsTest {
// Inject here
static class A {
private B b;
}
// Inject here also
static class B {
private C c;
}
// Some dependency
static class C {
}
@InjectMocks
private final A a = new A();
@Spy
@InjectMocks
private final B b = new B();
@Mock
private C c;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
// Worked in 2.0.12-beta
public void testArgumentShouldBeAMockButIsNonMock() {
b.toString();
}
}
Issue Analytics
- State:
- Created 8 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Is it discouraged to use @Spy and @InjectMocks on the same ...
Note: The reason why I think using both annotations together makes sense sometimes is because if you only use @InjectMocks Mockito tries to ......
Read more >Getting Started with Mockito @Mock, @Spy, @Captor and ...
We can use @Mock to create and inject mocked instances without having to call Mockito.mock manually. In the following example, we'll create a ......
Read more >Mockito: Why You Still Should Appreciate InjectMocks ...
We have to scan the test class first for fields with need to be mocked: those are annotated with @Mock , @Spy and...
Read more >Annotation Magic with Mockito: InjectMocks - Encora
Minimize repetitive mock and spy injection. Mockito will try to inject mocks only either by constructor injection, setter injection, or property injection, in ......
Read more >Creating Mocks and Spies in Mockito with Code Examples
Mocks and Spies are types of test doubles, which are helpful in writing unit tests. Both are explained in this Mockito Spy tutorial...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So what is Mockito official stance on using
@InjectMocks
with@Spy
? The javadoc still references it as being supported (https://github.com/mockito/mockito/blob/release/3.x/src/main/java/org/mockito/InjectMocks.java#L140) but it doesn’t work by using both annotations on a field. I don’t mind doing the workaround mentioned above withMockito.spy(new B())
instead of the annotations while this bug is being fixed but I’d like a definitive answer since that bug has been opened for a while now!Thanks!
I’ve just noticed that there is still this misbehavior (undocumented feature) when you provide spy object by hand. If you run test provided by @vbuell with object created by
Mockito#spy
method then test passes (of course it’s not important if there is @Spy annotation on that field).I’ve reproduced this issue on latest Mockito version: 2.7.14