I am doing JUnit tests with Mockito on Spring Mvc. The tests are using @InjectMock and @Mock with when(method(..)).thenReturn(X). The issue is how to @Mock methods that are within a @Inject instance?
I have tried creating two instances such as @InjectMocks Foo fooInstance and @Mock Foo fooInstanceMock; My way of thinking is to differentiate from what instance to inject and what to mock. I also tried using Spy with InjectMocks but it returns a exception.
Actual Class Syntax-
class Foo {
public X(..) {
...
Y(...); // method call to Y
...
}
public Y(..) {
...
}
}
Test Syntax -
public class FooTest {
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@InjectMocks
Foo fooInstance;
@Mock
Foo fooInstanceMock;
@Test
public void xTest{
when(fooInstanceMock.Y(..)).thenReturn(true);
Boolean result = fooInstance.X(25);
Assert.assertTrue(result == true)
}
}
I except the output to be true as when then return true but since it thinks it is a injectMock and it goes into the implementation.