Ability to set a Mockito type mock Database as the 'default' Database
ebean-orm / ebean-mocker Goto Github PK
View Code? Open in Web Editor NEWAbility to set a Mockito type mock EbeanServer as the 'default' EbeanServer
License: Apache License 2.0
Ability to set a Mockito type mock EbeanServer as the 'default' EbeanServer
License: Apache License 2.0
Currently MockiEbean can only be started with an EbeanServer mock.
Since this interface is deprecated, MockiEbean should also support the new Database interface.
For use:
EbeanServer mock = ...; // setup your mock or test double
MockiEbean.runWithMock(mock, new Runnable() {
@Override
public void run() {
...
// any test code in here uses the mock EbeanServer as the default server
}
});
This wraps an EbeanServer so that you can create a test double that just overwrites some methods but otherwise calls through to the underlying delegate EbeanServer.
So here TDCustFinder can be anything. Currently there is a restriction on the type of Finder but that is not ideal as there is Model.Finder and Finder now etc.
WithStaticFinder with = WithStaticFinder.use(Customer.class, new TDCustFinder());
try {
Customer jim = Customer.find.byName("jim");
} finally {
with.restoreOriginal();
}
Similarly we want to do (with any TDCustFinder)
DelegateEbeanServer server = new DelegateEbeanServer();
server.withFinder(Customer.class).as(new TDCustFinder());
// make TDCustFinder the finder for Customer
server.beforeRun();
try {
// run our test code ... that will use TDCustFinder
Customer jim = Customer.find.byName("jim");
} finally {
// restore the original finder implementation
server.afterRun();
}
I am trying to do the following in the tests
mockServer = mock(EbeanServer.class, withSettings().verboseLogging());
when(mockServer.getName()).thenReturn("default");
when(mockServer.toString()).thenReturn("Wazza");
mockiEbean = MockiEbean.start(mockServer);
and when i try to do a Ebean.getDefaultServer().toString() , it returns com.avaje.ebeaninternal.server.core.DefaultServer@405192e4
from the play controller, but within test it returns"Wazza"
as desired .
How can i get this running ?
the break-api is here: ebean-orm/ebean#647
in DelegateEbeanServer class is not implement method docStore method define in EbeanServer interface.
other quest: this project is step by step follow ebean with newest version?
in WithStaticFinder Line 46
public WithStaticFinder<T> as(Object testDouble) throws FinderFieldNotFoundException {
try {
this.testDouble = testDouble;
this.field = findField();
this.field.setAccessible(true);
try {
Field modifiersField = Field.class.getDeclaredField("modifiers");
/**
* If the project using this library has a SecurityManager set up, permission may be denied.
* Therefor, running this as a privileged action.
*/
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
modifiersField.setAccessible(true);
return null;
});
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
} catch (NoSuchFieldException e) {
// this fails with Java 17
throw new RuntimeException("Unable to turn off final field flag for " + field, e);
}
this.original = field.get(null);
return this;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
do we have any plan to update this for jdk17 or 21๏ผ
Hello @rbygrave ,
we are using now MockiEbean with ebean13 and we get a NoSuchMethodError
for getName()
in MockiEbean
, in the method restoreOriginal()
at line 201.
It should be original.name()
, the same for line 206.
Can you please fix it?
Kind regards,
Noemi
The following commit in avaje-ebeanorm changed EbeanServer to return boolean
instead of void
for two delete()
methods: ebean-orm/ebean@a578eda
Does the InterceptDelete interface in this project need to be updated to match the API change? It still has a void delete()
, but no boolean delete()
: https://github.com/ebean-orm/avaje-ebeanorm-mocker/blob/master/src/main/java/com/avaje/ebean/delegate/InterceptDelete.java
Such that you explicitly control the exact version of mockito in our project poms (and mockito is not strictly required when using MockiEbean etc.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.