I made a change in the blogger configuration to ease the later work when blogging. It is possible that older entries are not correctly formatted.

Showing posts with label mocks. Show all posts
Showing posts with label mocks. Show all posts

Wednesday, 27 June 2012

Mocking

Since I keep being annoyed at my inability to test classes quickly, I have to see if I can improve the speed of the testing using mocks. Mocks can be used in different ways. In particular they can be used in behavioral testing.

The idea us that instead of injecting existing classes in a code, one puts mocks which expect actions from the caller.

To illustrate the point, it is good to use an interface for the class called (and therefore to be mocked) and a class calling this interface. We first define the interface: Algorithm.

public interface Algorithm {
   void perform();
}

Then we define the AlgorithmUser class which calls the algorithm. We give it an algorithm.

public class AlgorithmUser {
  void performAlgorithm(Algorithm algorithm){
    algortihm.perform();
  }
}

After that we define a test and create a mock instance and define the expectations. To do this we use the JMock library.


@RunWith(JMock.class)
public class PublisherTest {

  private Mockery context = new JUnit4Mockery();

  @Test
  public void testPerformAlgorithm(Algorithm algorithm){
    // create the mock
    final Algorithm algorithm = context.mock(Algorithm.class);
    // create the expectations
    context.checking(new Expectations() {
      {
        oneOf(algorithm).perform();
      }
    });
    // perform
    new AlgorthmUser().performAlgorthm(algorithm);

  }
}

The power of mocks reside in the language which can be used for this checking.
To user jMock, there is a cheat sheet.


The different frameworks I know of:

Friday, 23 April 2010

Mockito - basics

This entry gives simple methods on how to use mockito.

Maven configuration

To add mockito-core to your configuration, update your dependencies with:

<!-- needs extra dependencies: objenesis & hamcrest -->
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.8.1</version>

In order to add the complete mockito framework, i.e. mockito-all, update your dependencies with

<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.1</version>

Of course if the mock code is only found in unit tests, then you might want to add:

<scope>test<scope>
to your pom.xml

Use of Mocks to verify interaction (Spying)

The following example is adapted from the mockito documentation

// the staic import allows the direct use of the help methods: mock and verify
import static org.mockito.Mockito.*;

// create a mock of a map
List mockedMap = mock(Map.class);

//using mock object
mockedMap.put("key", "value");
mockedMap.clear();
//verification
verify(mockedMap).put("key","value");
verify(mockedMap).clear();

Stubbing - Or Have your objects mock others

While the previous section was used to verify that some operation did occur on a specific object. In some cases, you may not wish to initialize completely a complex object. In such a case, you can use mockito to perform stubbing, i.e. mockito provides an interface, so that the object returns a specific value, when a call to a method is performed.

For instance, it is possible to use the mocking framework to return a given result for certain parameters:

when(mockedMap.get("key")).thenReturn("first");
when(mockedList.get(5)).thenThrow(new RuntimeException());

Thursday, 22 April 2010

Mock Frameworks

In this entry, I present a summary of the features of mock frameworks. In particular, I looked at two different mock frameworks: easyMock (MIT Licence), Mockito (Apache Licence) and JMock (probably equivalent to BSD, MIT or Apache but I am not completely sure). The Mockito framework seems to be trying to extend even more the features of the easy mock.

Usual Use Cases of Mock Objects

I adapted the following list from a wikipedia entry. It shows the cases where it a good practice to use mocks.

  • results of method do not follow a very deterministic pattern
  • The mock can simulate a complex object or an object difficult to obtain, e.g a given state in a GUI
  • A state of a system which is expensive to create, resource, processing, power...
  • The object has not yet been provided, perhaps by an other team

Here are some useful posts on mocks:

I notice this post is not at all too clean. It needs to explain the purpose of spying and the difference there is with mock objects which are only dummy object or fake object.