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() {
initXmi();
voucher = AbispulsaFactory.eINSTANCE.createVoucher();
voucher.setCode("I10");
voucher.setPrice(BigDecimal.valueOf(11500));
voucher.setCost(BigDecimal.valueOf(9900));
owner = new DummyOwner();
fixture.setRefillListener(createMock(RefillListener.class));
}

refillListener will be used somewhere in the implementation :

...
refill.setVoucher(voucher);
DealerImpl.this.getRefills().add(refill);
beforeSave(refill);
try {
refill.eResource().save(null);
} catch (IOException e) {
throw new AbispulsaException(e);
}
getRefillListener().created(refill);
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(EStructuralFeatureImpl.java:2337)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eDynamicGet(BasicEObjectImpl.java:1055)
at com.abispulsa.impl.DealerImpl.getRefillListener(DealerImpl.java:135)
at com.abispulsa.impl.DealerImpl$RefillTemplate.create(DealerImpl.java:85)
at com.abispulsa.impl.DealerImpl.refill(DealerImpl.java:288)
at com.abispulsa.tests.DealerTest.testRefill__Voucher_String_RefillOwner(DealerTest.java:351)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

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.

15 comments:

  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.

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

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  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?

    ReplyDelete
  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: http://www.ambersbridal.com

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


    Runescape Gold
    Buy Runescape Gold
    RS GP

    ReplyDelete
  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.

    ReplyDelete
  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.

    ReplyDelete
  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

    ReplyDelete
  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.

    ReplyDelete
  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

    ReplyDelete
  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.

    ReplyDelete
  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.

    ReplyDelete
  14. 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.

    ReplyDelete
  15. 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

    ReplyDelete