Skip to content

Selenium Test Caveat

Patrick Huang edited this page Oct 1, 2013 · 1 revision

If you use selenium to test your web app, you may want to set up some data before the test. This can be done with entityunit. However there are some things you may want to knwo when testing web app.

Assuming you use cargo to boot up your web app. Then the test start to run outside of the container. Entity unit requires an entity manager to persist made entities. Most container won't allow you to use entity manager (factory) outside (stackoverflow link). Therefore creating a separate entity manager factory in test scope is often the option. In other word, you have two entity manager factory in parallel operating on the same database. If this is the case, read on...

Server side detached entity

In some cases, your application/framework may choose to store some detached entity or their id somewhere on the server, i.e. in session or application scope. One common case is authenticated user entity. If you delete the entity between tests and re-create it with same value, the stored entity will cause EntityNotFound exception when binding back to context.

To avoid this problem:

  • Remove stored entity/id from session/application scope between tests (This may be hard to achieve)
  • Fix the entity id (see com.github.huangp.entityunit.entity.FixIdCallback javadoc or com.github.huangp.entityunit.entity.EntityMakerImplTest.java for more detail)

TODO: link zanata example

Second level cache enabled

If you enable second level cache in your app, entities created outside of container will not appear in server side cache. This is normally not a problem if your @Id is generated (each new entity will have different id and will never hid second level cache). But if you fix id of some entities, your server side second level cache will take effect. If between tests these entities always remain the same, this is fine. If not, or if you have enabled query cache as well, you may need to watch out some surprise test results.