Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Cannot I use Mock and InjectMocks together?

valid construction:

@InjectMocks
SomeClass sc = mock(SomeClass.class);

Invalid construction:

@InjectMocks
@Mock
SomeClass sc;

I want to inject mocks to another mock. I want to use only annotation style.

Why was in Mockito forbid second construction ?

Update

example:

    public class ArrTest {
    private SomeClass someClass;

    public List<String> foo(){
        anotherMethod(); // I suppose that this method works. I want to test it separately.
        //logic which I need to test
        return someClass.doSmth();// I suppose that this method works. I want to test it separately.
    }
    public void anotherMethod(){
        ///...
    }
}

public class SomeClass {
    public List<String> doSmth(){
        return null;
    }
}

test:

public class ArrTestTest {
    @InjectMocks
    ArrTest arrTest = Mockito.mock(ArrTest.class);
    @Mock
    SomeClass someClass;
    @Test
    public void fooTest(){        
         Mockito.when(someClass.doSmth()).thenReturn(new ArrayList<String>());
         Mockito.doNothing().when(arrTest).anotherMethod();
         System.out.println(arrTest.foo());
    }

}
like image 397
gstackoverflow Avatar asked Oct 20 '25 12:10

gstackoverflow


2 Answers

It sounds like you're trying to do something that doesn't really make sense. You shouldn't need to inject any dependencies into your mock since mocks by definition don't have any behaviour until you define it with when(mock.someMethod()).thenAnswer() or some variation.

(except perhaps if you're using a spy(), but you've specifically said you're using a @Mock).

Maybe you could explain your use case and why you're trying to inject dependencies into a mock?

like image 55
tones Avatar answered Oct 23 '25 02:10

tones


@InjectMocks specifically indicates that the annotated field will NOT contain a mock. Annotating @InjectMocks @Mock is not just unsupported—it's contradictory.

To return stubs wherever possible, use this:

@Mock(answer=Answers.RETURNS_DEEP_STUBS)
YourClass mockYourClassWithDeepStubs;

But heed the official documentation for this Answer:

WARNING: This feature should rarely be required for regular clean code! Leave it for legacy code. Mocking a mock to return a mock, to return a mock, (...), to return something meaningful hints at violation of Law of Demeter or mocking a value object (a well known anti-pattern).

Good quote I've seen one day on the web: every time a mock returns a mock a fairy dies.

like image 30
Jeff Bowman Avatar answered Oct 23 '25 02:10

Jeff Bowman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!