How to mock or instantiate App Engine Entity when unit testing

507 views
Skip to first unread message

Mayumi Liyanage

unread,
Oct 28, 2010, 12:31:22 PM10/28/10
to Google App Engine
In the project I'm working on we are using GWT + GAE. We are unit
testing using automocking container Jukito (http://code.google.com/p/
jukito/) which is build on top of Mokito (http://code.google.com/p/
mockito/) + JUnit.
The problem is unit testing the Mapper API. Since Mapper API's map
method takes an Entity as a argument so testing framework will have to
have access to this Entity class. However we could not mock the Entity
class since it is a final class. Also, we could not instantiate the
Entity class outside of GAE environment. '
Does anyone have workaround for unit testing using Entity?

Thanks.

Ikai Lan (Google)

unread,
Oct 28, 2010, 2:52:29 PM10/28/10
to google-a...@googlegroups.com
Do you need to mock Entity? Can you use a real instance of an Entity and check for state changes? You can mock out DatastoreService, so it might make more sense to write your expects() in those mocks instead. Entity classes only really have getProperty() and setProperty(), and in my opinion, it's not worth writing tests to see if these get called. What are you trying to do?

--
Ikai Lan 
Developer Programs Engineer, Google App Engine




--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.


Carlos Ble

unread,
Oct 28, 2010, 5:40:33 PM10/28/10
to Google App Engine
App Engine is so great that you can not write unit tests using
entities even if
you don't persist them. Just calling the constructor of an entity
makes all the datastore
stuff load which is slow. You do not want your unit test to take 1
minute to run, do you?
What we have done to be able to write unit tests is to not create
objects directly but
use a factory. So we have an EntitiesFactory which give us objects
which are not actual
entities but have the same properties. Apart from the benefit of
running tests fast and isolated (because they are not real entities
they can't alter database), we get a new layer that
makes it easy to migrate to other persistence mechanisms.

Cheers

On 28 oct, 19:52, "Ikai Lan (Google)" <ikai.l+gro...@google.com>
wrote:
> Do you need to mock Entity? Can you use a real instance of an Entity and
> check for state changes? You can mock out DatastoreService, so it might make
> more sense to write your expects() in those mocks instead. Entity classes
> only really have getProperty() and setProperty(), and in my opinion, it's
> not worth writing tests to see if these get called. What are you trying to
> do?
>
> --
> Ikai Lan
> Developer Programs Engineer, Google App Engine
> Blogger:http://googleappengine.blogspot.com
> Reddit:http://www.reddit.com/r/appengine
> Twitter:http://twitter.com/app_engine
>
> On Thu, Oct 28, 2010 at 9:31 AM, Mayumi Liyanage <
>
> mayumi.liyan...@coldwin.com> wrote:
> > In the project I'm working on we are using GWT + GAE. We are unit
> > testing using automocking container Jukito (http://code.google.com/p/
> > jukito/) which is  build on top of Mokito (http://code.google.com/p/
> > mockito/) + JUnit.
> > The problem is unit testing the Mapper API. Since Mapper API's map
> > method takes an Entity as a argument so testing framework will have to
> > have access to this Entity class. However we could not mock the Entity
> > class since it is a final class.  Also, we could not instantiate the
> > Entity class outside of GAE environment. '
> > Does anyone have workaround for unit testing using Entity?
>
> > Thanks.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google App Engine" group.
> > To post to this group, send email to google-a...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-appengi...@googlegroups.com<google-appengine%2Bunsu...@googlegroups.com>
> > .

Ikai Lan

unread,
Oct 28, 2010, 5:59:42 PM10/28/10
to google-a...@googlegroups.com
The Entity class is extremely lightweight:


It's just a class with two dependencies that aren't in the Java Standard Library:

- DataTypeUtils, which takes care of making sure properties can be persisted and converted
- a class that makes it compatible with our protobuf RPC implementation

Carlos, where are you seeing that com.google.appengine.api.datastore.Entity is slow? Can you post code?

--
Ikai



To unsubscribe from this group, send email to google-appengi...@googlegroups.com.

PhilBeaudoin

unread,
Oct 28, 2010, 10:49:25 PM10/28/10
to Google App Engine
We had a similar experience to Carlos. Before deciding to mock out the
entities we tried to instantiate them (with new) but got some errors
which seemed related to the fact that AppEngine was not available. I
was under the impression that it required instantiating AppEngine,
either because of static initializers or in the constructor. I will
look at the stack trace of that error and report back here.

Cheers,

Philippe

On Oct 28, 2:59 pm, Ikai Lan <i...@google.com> wrote:
> The Entity class is extremely lightweight:
>
> http://code.google.com/appengine/docs/java/javadoc/com/google/appengi...
> > On 28 oct, 19:52, "Ikai Lan (Google)" <ikai.l+gro...@google.com<ikai.l%2Bgro...@google.com>
> > > > google-appengi...@googlegroups.com<google-appengine%2Bunsubscrib e...@googlegroups.com>
> > <google-appengine%2Bunsu...@googlegroups.com<google-appengine%252Bunsub scr...@googlegroups.com>
>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/google-appengine?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google App Engine" group.
> > To post to this group, send email to google-a...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-appengi...@googlegroups.com<google-appengine%2Bunsubscrib e...@googlegroups.com>

mayumi

unread,
Oct 28, 2010, 10:56:58 PM10/28/10
to Google App Engine
I'm actually not testing the Entity itself or Appengine
DatastoreService.
Basically, I am converting the Entity to object I need than performing
some
operations to all the entity that is relevant.
What I am testing is the idempotence and making sure that object which
is converted
will result in same outcome event executed twice, etc so the behavior
of what is
happening inside the map().

Testing framework we're using requires Guice to inject or create mocks
as necessary.
Maybe this could be the problem?

Thanks for the reply.


On Oct 28, 1:52 pm, "Ikai Lan (Google)" <ikai.l+gro...@google.com>
wrote:
> Do you need to mock Entity? Can you use a real instance of an Entity and
> check for state changes? You can mock out DatastoreService, so it might make
> more sense to write your expects() in those mocks instead. Entity classes
> only really have getProperty() and setProperty(), and in my opinion, it's
> not worth writing tests to see if these get called. What are you trying to
> do?
>
> --
> Ikai Lan
> Developer Programs Engineer, Google App Engine
> Blogger:http://googleappengine.blogspot.com
> Reddit:http://www.reddit.com/r/appengine
> Twitter:http://twitter.com/app_engine
>
> On Thu, Oct 28, 2010 at 9:31 AM, Mayumi Liyanage <
>
> mayumi.liyan...@coldwin.com> wrote:
> > In the project I'm working on we are using GWT + GAE. We are unit
> > testing using automocking container Jukito (http://code.google.com/p/
> > jukito/) which is  build on top of Mokito (http://code.google.com/p/
> > mockito/) + JUnit.
> > The problem is unit testing the Mapper API. Since Mapper API's map
> > method takes an Entity as a argument so testing framework will have to
> > have access to this Entity class. However we could not mock the Entity
> > class since it is a final class.  Also, we could not instantiate the
> > Entity class outside of GAE environment. '
> > Does anyone have workaround for unit testing using Entity?
>
> > Thanks.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google App Engine" group.
> > To post to this group, send email to google-a...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-appengi...@googlegroups.com<google-appengine%2Bunsu...@googlegroups.com>
> > .

mayumi

unread,
Nov 19, 2010, 1:28:55 AM11/19/10
to Google App Engine
Problem: An Entity was a final class.
Solution: Use power mock or mocking container which let's you mock the
final classes.

Maicol Lastra

unread,
Oct 15, 2020, 6:44:51 PM10/15/20
to Google App Engine
Hi, I'm here 10 years later, I'd like to coverage my real  Datastore entities with mock using Junit, I don't know how to deal with that. Any help ?
Thanks in advance.

George (Cloud Platform Support)

unread,
Oct 16, 2020, 9:53:07 AM10/16/20
to Google App Engine
You may consider taking a look at the "Local Unit Testing for Java 8" documentation page, where actual and quite useful JUnit examples are provided. 
Reply all
Reply to author
Forward
0 new messages