question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Argument should be a mock, but is" error when combine @InjectMocks with @Spy

See original GitHub issue

In 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:open
  • Created 8 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
jebeaudetcommented, Sep 12, 2019

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 with Mockito.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!

6reactions
kcharlampowiczcommented, Mar 8, 2017

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

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
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 = Mockito.spy(new B());

	@Mock
	private C c;

	@Before
	public void setUp() throws Exception {
		MockitoAnnotations.initMocks(this);
	}

	@Test
	//Test passed
	public void testArgumentShouldBeAMockButIsNonMock() {
		b.toString();
	}
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found