In a previous
post, i promised to publish my own mockito code samples.Now, i have time to do that:
package mypackage;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyList;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
// some more imports..
public class MyMockitoTest {
private MockedClass mocked;
private TestedClass tested;
@Before
public void
setup() {
mocked= mock(MockedClass .class);
tested = new TestedClass (mocked);
}
@Test
public void
test1() {
// mocked method returns a new MockedReturnClass instance
when(mocked.test1CallsThis(anyString(), anyLong(), anyInt(), anyList(), any(MyClass.class)).thenReturn(new MockedReturnClass() ));
TestedReturnClass ret = tested.test1(//...test1 parameters);
assertNotNull(ret);
// required assertions
// verify what parameters test1CallsThis actually called with
verify(mocked).test1CallsThis("anyString", 2L, 1, ....);
}
@Test
public void test2() {
// mocked method returns a new, prepared MockedReturnClass instance, when called with THIS_PARAM
when(mocked.test2CallsThis(eq(EnumType.THIS_PARAM) )).thenAnswer(new Answer() {
public MockedReturnClass answer(InvocationOnMock invocation) throws Throwable {
MockedReturnClass retClass = new MockedReturnClass();
// prepare return data
return retClass ;
}
});
// mocked method returns a new MockedReturnClass instance, when called with THAT_PARAM
when(mocked.test2CallsThis(eq(EnumType.THAT_PARAM) )).thenReturn(new MockedReturnClass() ));
TestedReturnClass ret = tested.test2(//...test2 parameters);
assertNotNull(ret);
// required assertions
// verify both calls
verify(mocked).test2CallsThis(EnumType.THIS_PARAM);
verify(mocked).test2CallsThis(EnumType.THAT_PARAM);
}
@Test
public void test3() {
when(mocked.test3CallsThis(any(CapturedClass.class)).thenReturn(new MockedReturnClass() ));
TestedReturnClass ret = tested.test3(//...test3 parameters);
ArgumentCaptor captor = ArgumentCaptor.forClass(CapturedClass.class);
assertNotNull(ret);
// required assertions
//capture CapturedClass here
verify(mocked).test3CallsThis(captor.capture());
// and do required assertions
assertNotNull(captor.getValue());
// ... more assertions
}
@Test
public void test4() {
try {
// mocked method throws MockedException (return type of test4CallsThis is non-void )
when(mocked.test4CallsThis(anyString()).thenThrow(new MockedException() );
tested.test4(//...test4 parameters);
verify(mocked).test4CallsThis("anyString");
catch(MockedException ex) {
// ... do required assertions
}
@Test
public void test5() {
try {
// mocked method throws MockedException (return type of test5CallsThisTwiceis void)
doThrow(new MockedException()).when(mocked.test5CallsThisTwice(anyString());
tested.test5(//...test5 parameters);
verify(mocked, times(2)).test5CallsThisTwice("anyString");
catch(MockedException ex) {
// ... do required assertions
}
}
Hiç yorum yok:
Yorum Gönder