mockito etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
mockito etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

6 Temmuz 2015 Pazartesi

mockito with testng suite

When writing tests by mocking your objects, you have to call a method in order to make sure your @Mock annotations really work. This method call is as follows:
 MockitoAnnotations.initMocks(this);  


In general, we use a BaseTest and call the method there and extend our tests from that BaseTest. Thus, the tests succeed when you run them separately. However, when you call them as suite, you may get the folowing error:
 testClass cannot be null. For info how to use @Mock annotations see examples in javadoc for MockitoAnnotations class  


I tried several approaches to fix this including using MockitoTestNGListener.class, none of which work. Then i find an article advising not to use inheritance in tests (http://www.petrikainulainen.net/programming/unit-testing/3-reasons-why-we-should-not-use-inheritance-in-our-tests/), removing inheritance and calling MockitoAnnotations.initMocks(this);  inside every test class worked :)



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;



@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   
 }

}




14 Ekim 2011 Cuma

mockito argument capture

Here ise a good article about argument capture property of mockito: http://blog.james-carr.org/2009/09/28/mockito-verifying-details-of-an-object-passed-to-a-collaborator/

.. and here is more: http://docs.mockito.googlecode.com/hg/org/mockito/ArgumentCaptor.html
http://mockito.googlecode.com/svn/branches/1.8.0/javadoc/org/mockito/Mockito.html#15

when i have enough time, i will publish my own test examples..until then, i must say, unit testing with mockito framework is a great way of doing white box testing and this experience leads you to review & refactor your code accordingly..

7 Eylül 2011 Çarşamba

mockito


Mockito is a mocking framework that can be downloaded from http://code.google.com/p/mockito/

An article about mockito:
http://gojko.net/2009/10/23/mockito-in-six-easy-examples/
quote from that article

To create a stub (or a mock), use mock(class). Then use when(mock).thenReturn(value) to specify the stub value for a method. If you specify more than one value, they will be returned in sequence until the last one is used, after which point the last specified value gets returned. (So to have a method return the same value always, just specify it once).

Code samples on that blog are adequate, so i will not reinvent the wheel with more examples..