Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito throws an OutOfMemoryError on a simple test

I tried using Mockito to simulate a database pool (for retrieving data only), but when running a performance test that retrieved many mock connections over a period of time, it ran out of memory.

Here is a simplified self-contained code, which throws an OutOfMemoryError after about 150,000 loop iterations on my machine (despite that nothing seems to be saved globally, and everything should be garbage collectable). What am I doing wrong?

import static org.mockito.Mockito.when;

import java.sql.Connection;

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public class Test1 {

    static class DbPool {
        public Connection getConnection() {return null;}
    }

    @Mock
    private DbPool dbPool;

    @Mock
    private Connection connection;

    public Test1() {
        MockitoAnnotations.initMocks(this);
        when(dbPool.getConnection()).thenReturn(connection);

        for(int i=0;i<1000000;i++) {
            dbPool.getConnection();
            System.out.println(i);
        }
    }

    public static void main(String s[]) {       
        new Test1();
    }
}
like image 329
Tim Avatar asked Jul 02 '13 23:07

Tim


2 Answers

The response by david-wallace explains why you run into an OOM: a mock object is remembering details of every invocation.

But an equally important question is: now what to do about it? In addition to what David already suggested, the latest Mockito versions 1.10.19 as well as upcoming 2.0.x now support so-called stubOnly mocks (see javadoc):

stubOnly: A stub-only mock does not record method invocations, thus saving memory but disallowing verification of invocations.

Scala usage example:

import org.mockito.Mockito
val list = Mockito.mock(classOf[Foo], Mockito.withSettings().stubOnly())

// The syntax is a bit more concise when using ScalaTest's MockitoSugar
val foo = mock[Foo](Mockito.withSettings().stubOnly())

Java usage example (untested):

import org.mockito.Mockito;
Foo mock = Mockito.mock(Foo.class, Mockito.withSettings().stubOnly());
like image 139
Michael G. Noll Avatar answered Sep 17 '22 16:09

Michael G. Noll


The problem is that the mock object is remembering details of every invocation, in case you wish to verify it later. Eventually, it will inevitably run out of memory. What you need to do is occasionally reset the mock, using the Mockito.reset static method, and stub your method again. Unfortunately, there is no way to clear out a mock's verification information without also resetting the stubbing.

This issue is covered in detail at https://code.google.com/p/mockito/issues/detail?id=84

like image 43
Dawood ibn Kareem Avatar answered Sep 19 '22 16:09

Dawood ibn Kareem