Mockito

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Mockito is an open source testing framework for Java released under the MIT License.[1][2] The framework allows the creation of test double objects (mock objects) in automated unit tests for the purpose of Test-driven Development (TDD) or Behavior Driven Development (BDD).

In software development there is an opportunity of ensuring that objects perform the behaviors that are expected of them. One approach is to create a test automation framework that actually exercises each of those behaviors and verifies that it performs as expected, even after it is changed. However, the requirement to create an entire testing framework is often an onerous task that requires as much effort as writing the original objects that were supposed to be tested. For that reason, developers have created mock testing frameworks. These effectively fake some external dependencies so that the object being tested has a consistent interaction with its outside dependencies. Mockito intends to streamline the delivery of these external dependencies that are not subjects of the test. A research performed in 2013 on 10,000 GitHub projects found that Mockito is the 9th most popular Java library. [3]

Distinguishing features[edit]

Mockito distinguishes itself from other mocking frameworks by allowing developers to verify the behavior of the system under test (SUT) without establishing expectations beforehand.[4] One of the criticisms of mock objects is that there is a tight coupling of the test code to the system under test.[5] Since Mockito attempts to eliminate the expect-run-verify pattern[6] by removing the specification of expectations, the coupling is reduced or minimized. The result of this distinguishing feature is simpler test code that should be easier to read and modify. Mockito also provides some annotations useful for reducing boilerplate code.[7]

Origins[edit]

Szczepan Faber started the Mockito project after finding existing mock object frameworks too complex and difficult to work with. Faber began by expanding on the syntax and functionality of Easy Mock, but eventually rewriting most of Mockito.[8] Faber's goal was to create a new framework that was easier to work with and the Guardian project in London in early 2008.[9]

Usage[edit]

Mockito has a growing user-base[10][11] as well as finding use in other open source projects.[12]

Example[edit]

Consider this decoupled Hello world program; we may unit test some of its parts, using mock objects for other parts.

package org.examples;
 
import java.io.IOException;
 
public class HelloApplication {
 
   public static interface Greeter {
      String getGreeting(String subject);
      String getIntroduction(String actor);
   }
 
   public static class HelloGreeter implements Greeter {
      private String hello;
      private String segmenter;
 
      public HelloGreeter(String hello, String segmenter) {
         this.hello = hello;
         this.segmenter = segmenter;
      }
      public String getGreeting(String subject) {
         return hello + " " + subject; 
      }
      public String getIntroduction(String actor) {
         return actor+segmenter;
      }
   }
 
   public static interface HelloActable {
      void sayHello(String actor, String subject) throws IOException;
   }
 
   public static class HelloAction implements HelloActable {
      private Greeter helloGreeter;
      private Appendable helloWriter;
 
      public HelloAction(Greeter helloGreeter, Appendable helloWriter) {
         super();
         this.helloGreeter = helloGreeter;
         this.helloWriter = helloWriter;
      }
      public void sayHello(String actor, String subject) throws IOException { 
         helloWriter.append(helloGreeter.getIntroduction(actor)).append(helloGreeter.getGreeting(subject));
      }
   }
 
   public static void main(String... args) throws IOException {
      new HelloAction(new HelloGreeter("hello", ": "), System.out).sayHello("application", "world");
   }
 
}

The result of HelloApplication launching will be the following:

application: hello world

Unit test for HelloActable component may look like this:

package org.examples;
 
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
import org.junit.Before;
import org.junit.Test;
 
import org.examples.HelloApplication.HelloActable;
import org.examples.HelloApplication.HelloAction;
import org.examples.HelloApplication.Greeter;
 
public class HelloActionUnitTest {
 
   Greeter helloGreeterMock;
   Appendable helloWriterMock;
   HelloActable helloAction;
 
   @Before
   public void setUp() {
      helloGreeterMock = mock(Greeter.class);
      helloWriterMock = mock(Appendable.class);
      helloAction = new HelloAction(helloGreeterMock, helloWriterMock);
   }
 
   @Test
   public void testSayHello() throws Exception {
      when(helloWriterMock.append(any(String.class))).thenReturn(helloWriterMock);
      when(helloGreeterMock.getIntroduction(eq("unitTest"))).thenReturn("unitTest : ");
      when(helloGreeterMock.getGreeting(eq("world"))).thenReturn("hi world");
 
      helloAction.sayHello("unitTest", "world");
 
      verify(helloGreeterMock).getIntroduction(eq("unitTest"));
      verify(helloGreeterMock).getGreeting(eq("world"));
 
      verify(helloWriterMock, times(2)).append(any(String.class));
      verify(helloWriterMock, times(1)).append(eq("unitTest : "));
      verify(helloWriterMock, times(1)).append(eq("hi world"));
   }
}

It uses mock objects for the Greeter and Appendable interfaces, and implicitly assumes the next use case:

unitTest : hi world

Integration test code for testing HelloActable wired together with Greeter may look like the following:

package org.examples;
 
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
import org.junit.Before;
import org.junit.Test;
 
import org.examples.HelloApplication.HelloActable;
import org.examples.HelloApplication.HelloAction;
import org.examples.HelloApplication.Greeter;
import org.examples.HelloApplication.HelloGreeter;
 
public class HelloActionIntegrationTest {
   HelloActable helloAction;
   Greeter helloGreeter;
   Appendable helloWriterMock;
 
   @Before
   public void setUp() {
      helloGreeter = new HelloGreeter("welcome", " says ");
      helloWriterMock = mock(Appendable.class);
      helloAction = new HelloAction(helloGreeter, helloWriterMock);
   }
 
   @Test
   public void testSayHello() throws Exception {
      when(helloWriterMock.append(any(String.class))).thenReturn(helloWriterMock);
 
      helloAction.sayHello("integrationTest", "universe");  
 
      verify(helloWriterMock, times(2)).append(any(String.class));
      verify(helloWriterMock, times(1)).append(eq("integrationTest says "));
      verify(helloWriterMock, times(1)).append(eq("welcome universe"));
   }
}

It uses mock objects only in place of Appendable interfaces, and uses the real implementations for other (HelloActable and Greeter) interfaces, and implicitly assumes the next use case:

integrationTest says welcome universe

As can be seen from the import statements of HelloActionUnitTest and HelloActionIntegrationTest classes, it is necessary to put some Mockito jars and JUnit jars in your class path to be able compile and run the test classes.

See also[edit]

References[edit]

  1. ^ "Mockito in six easy examples". 2009. Retrieved 2012-10-05. 
  2. ^ "What's the best mock framework for Java?". Retrieved 2010-12-29. 
  3. ^ Weiss, Tal (2013-11-26). "GitHub’s 10,000 most Popular Java Projects – Here are The Top Libraries They Use". Retrieved 2014-03-11. 
  4. ^ "Features and Motivations". Retrieved 2010-12-29. 
  5. ^ Fowler, Martin (2007). "Mocks Aren't Stubs". Retrieved 2010-12-29. 
  6. ^ Faber, Szczepan. "Death Wish". Retrieved 2010-12-29. 
  7. ^ Kaczanowski,, Tomek. "Mockito - Open Source Java Mocking Framework". Retrieved 2013-09-17. 
  8. ^ Faber, Szczepan. "Mockito". Retrieved 2010-12-29. 
  9. ^ "Mockito Home Page". Retrieved 2010-12-29. 
  10. ^ http://www.google.com/trends/explore#q=mockito,easymock,jmock
  11. ^ "Mockito User Base". Retrieved 2010-12-29. 
  12. ^ "Mockito in Use". Retrieved 2010-12-29. 

External links[edit]