Add maven dependency:
<dependency>
<groupId>org.mockito </groupId>
<artifactId>mockito-all </artifactId>
<version>1.8.5 </version>
<scope>test </scope>
</dependency>
First you have to specify the following:
@RunWith(MockitoJUnitRunner.class)
public class YourTest
To mock any object you can use 2 approaches:
@Mock
private YourObject yourObject;
or
YourObject yourObject = mock(YourObject.class);
Verify operations example:
when(classField.getSpecificMethod()).thenReturn(stub value);
verify(classField).getSpecificMethod();
verifyNoMoreInteractions(classField);
verify(classField, atLeast(1)).getSpecificMethod();
*atLeast(1) can be replaced with: atLeastOnce(), never()Exception generation:
YourException yourException = mock(YourException.class);
doThrow(yourException).when(classField).getSpecificMethod();
when(classField.getSpecificMethod(any(Class.class),anyInt())).thenThrow(new YourException ());
No comments:
Post a Comment