• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Use of mock objects to test classes that use concrete classes

 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I just started working on Mock objects. it's a nice tool, but am facing a problem. It looks like Mock objects can only mimics interfaces but not concrete class..

More specifically when I create control class
mockClientcontrol = MockControl.createControl(AClass.class);
AClass should be interface.

In my case AClass is concrete class. Is there anyway that Mock object can be created for a Class in the same way we do for interface?

Your help is appreciated.
Manohar
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like you are using EasyMock, correct?

Well, there was some discussion on the EasyMock list about also allowing mocks created for classes, but I don't remember the outcome. If you are interested, you should take a look at the EasyMock mailing list archive.

Of course, you could still implement your Mock object manually, simply by extendending the class you want to mock. This is a rather brittle solution, though, as changes in the original class could easily break your mock.

If the source of the class you want to test is under your control, it would certainly be best if you simply introduced an interface...
 
Manohar Karamballi
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a requirement where my class that is being tested instantiates thrid party class which is not implementing any interface.

Am just not getting how to test this..either manually or using any API for the same.

Pls help me out.

Also please let me know whether there is any easy to understand and complete mock object tutorial kinds thing.

Thanks in advance
Manohar
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The latest release of EasyMock supports mocking up concrete classes (it uses CGLIB bytecode library internally to accomplish this). I haven't yet used the class mocking feature myself, though, so I can't comment on its use.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manohar Karamballi:
I have a requirement where my class that is being tested instantiates thrid party class which is not implementing any interface.

Am just not getting how to test this..either manually or using any API for the same.


If you can't test the class because you can't mock up the 3rd party class, separate those two from each other and only test the part you can test. It's perfectly fine as long as you keep the "glue code" between your class and the 3rd party class as thin as possible.
 
Manohar Karamballi
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The latest release of EasyMock supports mocking up concrete classes (it uses CGLIB bytecode library internally to accomplish this).



Is that beta version? I am working on latest version but when tried to issue MockControl.createControl(MockHttpClient.class); test is failing with message "MockHTTPClient" is not interface.

Am I using wrong syntax? or else wrong version of easy mock? if so please let me know correct version. FYI, I am using easymock 1.0.1

Manohar
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manohar Karamballi:
I have a requirement where my class that is being tested instantiates thrid party class which is not implementing any interface.

Am just not getting how to test this..either manually or using any API for the same.



In this case I would simply let my class work on an interface and introduce an Adapter for the thirdparty class. The Adapter will probably be so simple that it doesn't need any tests.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manohar, did you download the EasyMock Class Extension in addition to the regular EasyMock distribution?
 
Manohar Karamballi
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I did..that jar file is in classpath.
I was unable to cast Concrere class as I was getting NoClassDefFounf error..

Thanks for Forum members wonderful support

Manohar
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you download CGLIB as well?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manohar Karamballi:
mockClientcontrol = MockControl.createControl(AClass.class);


By the way, the line above should read
mockClientcontrol = MockClassControl.createControl(AClass.class);
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic