Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

testing a func that uses another class(which contains static and instance methods)

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


please tell me how to test foo() and foo2() in MyClient.


thanks.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change your design, so that MyClient can work with a MockServer for testing purposes.
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for replying.

i cannot touch the server, its a Legacy class but then atleast tell me how to redesign MyClient so i can test it,

tell me how can i mock a class that has both static and instance methods???/

an explanatory answer stating "WHAT-TO-REDESIGN" will be more helpful.

thanks,
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's one possible approach for testing foo():

Now, before you call MyClient.doStaticStuff(), you choose the adapter implementation by calling MyClient.setServerAdapter(new RealAdapter()) or MyClient.setServerAdapter(new MockAdapter());
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for a gr8 reply,

but if u have noticed my server has instance methods too. and a constructor that takes parameters???/

so how do u suggest i implement that part using a mock object (or adapter.)???

here the main problem is creating an adapter for a class that has both static and instance methods???

how to do that , a code example will be very helpful.

thanks.
 
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
By introducing a simple factory class, you can (again) get away from 'new Server'. Then you (again) introduce a setter for replacing the real thing with a mock implementation.

[ November 18, 2004: Message edited by: Lasse Koskela ]
 
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
Lasse is right on track. As the saying goes: "In software development, every problem can be solved by introducing another layer of indirection."
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a millons , for taking pains to read this post and replying.


i m totally confused here,

both the code examples u gave here are perfect examples when viewed in isolation.

but as i mentioned earlier my Server class has both "static and non-static methods" and i dont know still how all this will solve my prob.





now what if by mistake i say ServerAdapterFactory.newAdapter().doInstanceStuff)();
then i m totally scrwed???


Are u able to get my point here???

all ur explanation and code eg are very good .

but they still dont allow me to use RealAdapter in static and nonstatic context.
because the RealAdapter has no static methods only instance methods.

and if i create an object of RealAdapter() (using factory ofcourse) for the purpose
of calling the RealAdapter.doStaticStuff(), then i m not passing "ComplicatedObject cplx, ..."
to the constructor, (cause Server.doStaticStuff() is static )


but now still i can call the RealAdapter().doInstanceMethod() on this object, cause its an instance method.
and obviously , i dont want to allow that.


Are u getting my point???

if not then ask me further questions.

or better give me complete e.g. of implementing foo() and foo2() method both TOGETHER. cause in seperate isolated e.g. u r not solving my problem.


if u think writing the whole code e.g together (again) for someone as stupid as me is not worth ur time, then plz answer this question?

interface ServerAdapter {
void doStaticStuff();
void doInstanceStuff();
}

is this the correct definition of ServerAdapter Interface???
if yes, then every object that implements this interface is forced to provide implementation of doInstanceStuff(); which is not possible.

how do u now suggest i solve this problem???


thanks a millons , for taking pains to read this post and replying.
 
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 Jigar Gosar:
now what if by mistake i say ServerAdapterFactory.newAdapter().doInstanceStuff)();
then i m totally scrwed?

No, you're not screwed. You just fix your code.

Originally posted by Jigar Gosar:
but they still dont allow me to use RealAdapter in static and nonstatic context. because the RealAdapter has no static methods only instance methods.

The whole point about introducing the layer of indirection between the client code and the server code is to get rid of static methods and replace them with instance methods that you can mock with a fake implementation for testing purposes. Those instance methods can then invoke the static method!

but now still i can call the RealAdapter().doInstanceMethod() on this object, cause its an instance method. and obviously , i dont want to allow that.

Life is full of compromises. I'm afraid this might be one of them. Sometimes it's acceptable to rely on instructions and documentation instead of technical restrictions.

interface ServerAdapter {
void doStaticStuff();
void doInstanceStuff();
}

is this the correct definition of ServerAdapter Interface?

Yes, that is a correct definition of an interface.

if yes, then every object that implements this interface is forced to provide implementation of doInstanceStuff(); which is not possible.

how do u now suggest i solve this problem?


Why do you say it's not possible? Here's a perfectly valid implementation of the above interface:


The point is, even though the interface says that the object knows how to doInstanceStuff(), nobody will come and take away your IDE if you don't implement doInstanceStuff() semantically correct.

Here's the whole thing in one file named StaticNonStaticExample.java:
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot , for making it all clear.

to me it dosent sound good, to thow IllegalStateException,
but i think no other way is possible.

so i'll implement this solution.

u have been a gr8 help.

thanks.

i m a relatively new at Testing, so i'll be comming here with some other questions. and i hope, u'll be there to help me out.

thanks (again ).
 
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
Another thing you can do is applying the Interface Segregation Principle. It sounds as if the static methods of the Server class are used significantly differently (to different occasions), so perhaps the methods should be in different interfaces. For example, have a StaticServerStuff and an InstanceServerStuff interface (I hope in your actual code you are using much better names than those ).

It's hard to give more specific advice without knowing more about the code, though: what are the methods of the Server class doing?
 
Goodbye moon men. Hello tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic