Reputation: 8933
Does @Mock and @InjectMock create a new mock value for every test in my test class ? Wondering how this works and if it doesn't create a new mock value do I have to use reset ?
Upvotes: 1
Views: 383
Reputation: 7336
@Mock is shorthand for Mockito.mock(Foo.class);
You use this to initialize a mock.
@InjectMocks is shorthand for MockitoAnnotations.initMocks(this)
You use this to set initialize your class with all of the mocks you created!
This will give you a clean setup for every test (@Test) you write.
public class FooTest {
private Foo foo = new Foo();
@Mock
private Bar bar;
@Before
public void setup() {
initMocks(this);
}
@Test
public void testSetupOk() { // delete this test once it passes
assertNotNull(foo);
assertNotNull(bar); // this will fail if you remove @Mock
}
}
You use these annotations together to properly setup the class under test.
Note that you still have to configure your mocks to return the desired behavior! (see Mockito.when in the API documentation).
Upvotes: 2
Reputation: 13181
According to MockitoJUnitRunner
's documentation:
Mocks are initialized before each test method.
Upvotes: 1