public class MockiEbean extends Object
This enables a developer to write a test using a tool like Mockito to mock the EbeanServer interface and make this the default server of the Ebean singleton.
EbeanServer mock = ...; // create a mock or test double etc
MockiEbean.runWithMock(mock, new Runnable() {
public void run() {
...
// test code in here runs with mock EbeanServer
}
});
An example using Mockito to mock the getBeanId() method on EbeanServer.
@Test
public void testWithMockito() {
EbeanServer defaultServer = Ebean.getServer(null);
assertTrue("is a real EbeanServer", defaultServer instanceof DefaultServer);
Long magicBeanId = Long.valueOf(47L);
EbeanServer mock = Mockito.mock(EbeanServer.class);
when(mock.getBeanId(null)).thenReturn(magicBeanId);
MockiEbean mockiEbean = MockiEbean.start(mock);
try {
// So using the Ebean singleton returns the mock instance
EbeanServer server = Ebean.getServer(null);
Object beanId = server.getBeanId(null);
assertEquals(magicBeanId, beanId);
} finally {
mockiEbean.restoreOriginal();
}
EbeanServer restoredServer = Ebean.getServer(null);
assertTrue("is a real EbeanServer", restoredServer instanceof DefaultServer);
}
| Modifier and Type | Method and Description |
|---|---|
void |
afterRun()
Typically only used internally.
|
void |
beforeRun()
Typically only used internally.
|
com.avaje.ebean.EbeanServer |
getMock()
Return the mock EbeanServer instance that was set as the default EbeanServer.
|
com.avaje.ebean.EbeanServer |
getOriginal()
Return the original EbeanServer implementation.
|
void |
restoreOriginal()
Restore the original EbeanServer implementation as the default EbeanServer.
|
<V> V |
run(Callable<V> testCallable)
Run the test callable restoring the original EbeanServer afterwards.
|
void |
run(Runnable testRunnable)
Run the test runnable restoring the original EbeanServer afterwards.
|
static <V> V |
runWithMock(com.avaje.ebean.EbeanServer mock,
Callable<V> test)
Run the test runnable using the mock EbeanServer and restoring the original EbeanServer afterward.
|
static void |
runWithMock(com.avaje.ebean.EbeanServer mock,
Runnable test)
Run the test runnable using the mock EbeanServer and restoring the original EbeanServer afterward.
|
static MockiEbean |
start(com.avaje.ebean.EbeanServer mock)
Set a mock implementation of EbeanServer as the default server.
|
public static MockiEbean start(com.avaje.ebean.EbeanServer mock)
Typically the mock instance passed in is created by Mockito or similar tool.
The default EbeanSever is the instance returned by Ebean.getServer(String) when the
server name is null.
mock - the mock instance that becomes the default EbeanServerrestoreOriginal() method that can be used to restore the
original EbeanServer implementation.public static void runWithMock(com.avaje.ebean.EbeanServer mock, Runnable test)
mock - the mock instance that becomes the default EbeanServertest - typically some test code as a runnablepublic static <V> V runWithMock(com.avaje.ebean.EbeanServer mock, Callable<V> test) throws Exception
mock - the mock instance that becomes the default EbeanServertest - typically some test code as a callableExceptionpublic com.avaje.ebean.EbeanServer getOriginal()
This is the implementation that is put back as the default EbeanServer when
restoreOriginal() is called.
public com.avaje.ebean.EbeanServer getMock()
public void run(Runnable testRunnable)
public <V> V run(Callable<V> testCallable) throws Exception
Exceptionpublic void beforeRun()
public void afterRun()
public void restoreOriginal()
Copyright © 2016. All rights reserved.