12 Aralık 2011 Pazartesi

mockito code samples

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;


public void setup() {

   mocked= mock(MockedClass .class);
   tested = new TestedClass (mocked);

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);
     // required assertions
     // verify what parameters test1CallsThis actually called with      
     verify(mocked).test1CallsThis("anyString", 2L, 1, ....);      

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);
     // required assertions
     // verify both calls  

public void test3() {
     when(mocked.test3CallsThis(any(CapturedClass.class)).thenReturn(new MockedReturnClass() ));
     TestedReturnClass ret = tested.test3(//...test3 parameters);
     ArgumentCaptor captor = ArgumentCaptor.forClass(CapturedClass.class);

     // required assertions
     //capture CapturedClass here
     // and do required assertions
     // ... more assertions


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);
     catch(MockedException ex) {
     // ... do required assertions   


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