MockiEbean

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;
...

@Test
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
  when(mock.getBeanId(null)).thenReturn(someBeanId);

  // ---------------
  // '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
    // ---------------
    mockiEbean.restoreOriginal();
  }

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

MockiEbean Maven dependency

<dependency>
  <groupId>io.ebean</groupId>
  <artifactId>ebean-mocker</artifactId>
  <version>11.17.1</version>
  <scope>test</scope>
</dependency>