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();
}
}
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());
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With