Mockito

From Null-pointer

Jump to: navigation, search

Contents

maven

<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-all</artifactId>
  <version>1.8.2</version>
  <scope>test</scope>
</dependency>

applicationContext.xml

<bean id="myObject" name="myObject" class="org.mockito.Mockito" factory-method="mock">
   <constructor-arg value="null-pointer.MyObject"/>
</bean>

DispatcherServlet

If testing using a Dispatcher servlet

public class MockServletBase {
 
    protected static final String JSP_PREFIX = "/WEB-INF/jsp/";
    protected static final String JSP_SUFFIX = ".jsp";
    protected static DispatcherServlet ds;
    protected static MockServletConfig servletConfig;
 
    protected MyObject myObjectMock;
 
    public void setUp() throws Exception {
        MockServletContext msc = new MockServletContext();
 
        servletConfig = new MockServletConfig(msc);
        ds = new DispatcherServlet();
        ds.setNamespace("applicationContext-test");
        ds.init(servletConfig);
        ApplicationContext applicationContext =  ds.getWebApplicationContext();
        myObjectMock = (MyObject) applicationContext.getBean("myObject");
    }
}

@Mock

public class MyControllerTest extends MockServletBase {
   @Mock MyObject myObjectMock;
 
   @Before
   public void setUp() throws Exception {
     MockitoAnnotations.initMocks(this);
   }
}

Example test

This example demostrates:

  • when(...)
  • doThrow(...) needed to void return methods
  • Argument Capture
public class MyControllerTest extends MockServletBase {
    @Before
    public void setup() {
        super.setup();
    }
 
    @Test
    public void testUpdateGroupNewGroup() throws Exception {
        Group group = new Group();
        group.setId(0);
 
        String name = "my new group";
        String description = "my description";
 
        when(myObjectMock.createGroup((Group) anyObject())).thenReturn(group);
 
        MockHttpServletRequest request = new MockHttpServletRequest("POST", "/group");
        request.setParameter("id", "0");
        request.setParameter("name", name);
        request.setParameter("description", description);
        MockHttpServletResponse response = service(request);
        assertNotNull(response.getRedirectedUrl());
 
        ArgumentCaptor<Group> argument = ArgumentCaptor.forClass(Group.class);
        verify(aceClientMock).createGroup(argument.capture());
        assertEquals(name, argument.getValue().getName());
        assertEquals(description, argument.getValue().getDescription());
    }
 
    @Test
    public void testException() throws Exception {
        doThrow(new Exception("test")).when(aceClientMock).deleteUser(anyInt());
        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/user/159/delete");
 
        MockHttpServletResponse response = service(request);
        assertEquals(404, response.getStatus() );
 
 
    }
}

Verify

//using mock 
 mockedList.add("once");
 
 mockedList.add("twice");
 mockedList.add("twice");
 
 mockedList.add("three times");
 mockedList.add("three times");
 mockedList.add("three times");
 
 //following two verifications work exactly the same - times(1) is used by default
 verify(mockedList).add("once");
 verify(mockedList, times(1)).add("once");
 
 //exact number of invocations verification
 verify(mockedList, times(2)).add("twice");
 verify(mockedList, times(3)).add("three times");
 
 //verification using never(). never() is an alias to times(0)
 verify(mockedList, never()).add("never happened");
 
 //verification using atLeast()/atMost()
 verify(mockedList, atLeastOnce()).add("three times");
 verify(mockedList, atLeast(2)).add("five times");
 verify(mockedList, atMost(5)).add("three times");

See Also

Personal tools