Sunday, January 2, 2011

Unit Testing EMF Models with EasyMock

During unit testing, I use EasyMock to mock an EMF-generated interface (I don't plan on persisting the interface nor its implementation).

Example usage is like this:

protected void setUp() {
voucher = AbispulsaFactory.eINSTANCE.createVoucher();
owner = new DummyOwner();

refillListener will be used somewhere in the implementation :

try {
} catch (IOException e) {
throw new AbispulsaException(e);
return refill;

The intention is to use a mock object for refillListener attribute/interface so I don't have to provide a real object.

But I got this error:

java.lang.ClassCastException: $Proxy0 cannot be cast to org.eclipse.emf.ecore.InternalEObject
at org.eclipse.emf.ecore.impl.EStructuralFeatureImpl$InternalSettingDelegateSingleEObject.dynamicGet(
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eDynamicGet(
at com.abispulsa.impl.DealerImpl.getRefillListener(
at com.abispulsa.impl.DealerImpl$RefillTemplate.create(
at com.abispulsa.impl.DealerImpl.refill(
at com.abispulsa.tests.DealerTest.testRefill__Voucher_String_RefillOwner(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at junit.framework.TestCase.runTest(
at junit.framework.TestCase.runBare(
at junit.framework.TestResult$1.protect(
at junit.framework.TestResult.runProtected(
at junit.framework.TestSuite.runTest(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(

The solution is to mark the attribute as Resolve Proxies = false. (In addition, I also mark Transient = true)

I'm not sure how this affects persistence mechanism such as Teneo or CDO, and if this is the proper way or not. If you know the proper way please advise. Thanks.


  1. Not a solution but never mock value objects, in this sense EMF Pojos!
    Without knowing any domain, rehtink also if RefillListener should be really an EMF object.

  2. @mattre Thank you, I guess that's way to least amount of problems! :)

  3. This comment has been removed by the author.

  4. Hi Hendy,
    The issue I have is not directly relevant to this post.
    I have generated an EMF object model derived from XML schema. I can edit it and add new attributes and add new child elements to the object model. But I can't add text contents to the objects which should be possible based on the schema.
    eg - <name></name> -> <name>bob</name>

    Anyway suppose my xml contains <name>alice</name>, then I can refer it using
    name.getMixed().getValue(0). As well I can replace the content using name.getMixed().setValue(0, "NewName")
    Do you know how to add new text content in such cases?

  5. Thanks for sharing – There are couple of others which I think deserve a mention like 99 Designs etc. It will be great if you could also feature some UX sources in the list! Cheers
    For more info on wedding dresses 2012 and lace wedding gowns, please visit us at:

  6. thank you for your current write-up,Our dilemma has been settled.

    Runescape Gold
    Buy Runescape Gold
    RS GP

  7. I've created the EMF subject style Runescape Gold derived from XML schema. I will change the idea as well as add fresh attributes and also include fresh little one components on the thing design. However are unable to increase textual content contents to the things that ought
    www.mmohome.comto always be possible using the schema.

  8. The OBD two devices can offer information as vast ranging as currently being in a position to monitor motor temperature to such things as tire pressure along with other automobile connected circumstances.

  9. Very cool. I always take advantage of the selection interviews. Many thanks for having this website Buy D3 Items
    Billig Guild wars 2 CD KEY

  10. the ball far distance from the archer to the remote treatment and tanks to eat a long time without the ball close to the archers.

  11. Alchemy is a profitable professional buy gw2 gold skill which creates their own agents, or sells items to other people, but also sends some to the teammates and friends

  12. Specifically, the number of cheap gw2 gold players has dropped to 8 million after losing 1.3 million in the last year. This confirms that the latest expansion which became a positive effect on the game was developed by Blizzard Mists of Pandaria, with sales in the first week reached 2.7 million FIFA 13 ultimate team coins units.

  13. In this situation, its creators have announced that wow gold they will give up the game and it will release the 5.3 patch , which will become a content update in Azeroth and Outland since the runescape money arrival of Pandaria.

  14. You wouldn't think that collaboration could increase through something as simple and ancient as copying and pasting Project information. With this new functionality, you can now copy and paste content to and from Office programs and Project 2010 and keep its formatting, outline levels, and column headers.
    Adobe Acrobat 9 Download is a platform for viewing, creating, manipulating and managing files in Adobe's PDF format.
    Visio Premium 2010 takes diagramming to a bold new level with dynamic, data-driven visualization tools and templates, powerful process-management features, and advanced sharing capabilities through the web. Bring the big-picture and real-time data from multiple sources, including Microsoft Excel 2010 and Microsoft SQL Server, together in one powerful diagram with vibrant graphics such as icons, colors, and data bars.

  15. Specifically, it is a dubstep button bolsillo.Aparte of all these extras will add all game objects rs gold 2007 editing Commander reservation, which includes a Screaming Eagle aircraft vertical takeoff rocket firing claws and you can invoke the attack "Sonic Scream," a uniform based on the popular costume "Uncle Sam" and a powerful weapon, the collector's edition Gun.Esta Merica and can be reserved. It is priced at 99.99 euros on Xbox 360 and PlayStation 3 best place to buy rs gold, or 89.99 euros in PC.

  16. I wish to say that this article is amazing, nice written and come with almost all important infos. I would like to read more posts like this .

    recover iPhone deleted text messages
    recover deleted text messages from iPhone 4

  17. Nice article, thanks for the information. It's very complete information. I will bookmark for next reference
    jaring futsal | jaring golf | jaring pengaman proyek |
    jaring pengaman bangunan | jaring pengaman gedung