MockiEbean from ebean-mocker project provides a helper object to support mocking the EbeanServer using tools like Mockito. If you like the Play/Active record style or Ebean singleton style you can add a test dependency on ebean-mocker and use MockiEbean to enable use of Mockito and similar tools. Mocking with Ebean singleton MockiEbean from ebean-mocker provides a mechanism for using a tool like Mockito and replacing the default EbeanServer instance with a mock.

import io.ebeaninternal.server.core.DefaultServer;

public void testWithMockito() {

  EbeanServer defaultServer = Ebean.getServer(null);
  assertTrue("is a real EbeanServer", defaultServer instanceof DefaultServer);

  Long someBeanId = Long.valueOf(47L);

  // Use Mockito to create a mock for the EbeanServer interface
  EbeanServer mock = Mockito.mock(EbeanServer.class);

  // setup some required behaviour

  // ---------------
  // 'register' the mock instance into Ebean
  // this becomes the 'default EbeanServer' until
  // mockiEbean.restoreOriginal() is called
  // ---------------
  MockiEbean mockiEbean = MockiEbean.start(mock);
  try {

    // Ebean singleton 'default server' now returns the mock instance
    EbeanServer server = Ebean.getDefaultServer();

    // always returns the someBeanId setup by Mockito
    Object beanId = server.getBeanId(null);

    assertEquals(someBeanId, beanId);

  } finally {
    // ---------------
    // restore the original defaultServer instance
    // ---------------

  EbeanServer restoredServer = Ebean.getDefaultServer();
  assertTrue("is a real EbeanServer", restoredServer instanceof DefaultServer);

MockiEbean Maven dependency