• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Tools for testing static methods

 
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know of any testing tools that will Unit test static methods?
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mock object
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's a mock object, Don? I've heard of a Mock Turtle, but mock object never. Could you explain in full detail?
Thanks
-Barry
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mock object
it is pretty new to me. if this is not good enough, just do s search on google.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JUnit
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mock Objects is perhaps the best resource on the net.
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure Mock Objects will allow you to test static methods? In working w/Mock Objects, it is to my understanding that these objects mock the original objects so you can stub in the method calls. Does this hold for static methods as well?
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dale DeMott:
Are you sure Mock Objects will allow you to test static methods? In working w/Mock Objects, it is to my understanding that these objects mock the original objects so you can stub in the method calls. Does this hold for static methods as well?


i can not see static methods make big difference here...but i am not sure.
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Usually with mock objects you create an interface that has the same method calls as your original object that you are testing. Then you have both your mock object and your original object implement the same interface. Then you have your program use this interface instead of your real object. This allows you to switch out the real object and the mock object without your program knowing objects have been switched out.
Car implements ICar
MockCar implements ICar
ICar car = new MockCar();
//The code below wouldn't know if 'car' is a mock or not.
This is usually how mock objects are used. (At least in our shop) This leaves out the ability to test static methods. Does anyone else have another method that might be more elegant?
[ May 08, 2003: Message edited by: Dale DeMott ]
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, i just start reading on mock object, but i am sure you will fiind better answer in test forum.
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For MockObjects, I have already looked for a solution. I was wondering if someone found something other than MockObjects that can help you with this. (And yes. Cindy should move this to the Testing forum)
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just what I was thinking. Moving to the Testing Forum.
 
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused by this thread. I fail to see what makes unit testing static methods any harder than unit testing non-static methods...
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I didn't word this quite as clear as I would have liked to. Unit testing a static method is very easy. Your quite right that this would be no harder than testing a non-static method. However if you were testing a subsystem with something like MockObjects, how might you test a system with static methods in the dependant classes. As you probably know, MockObjects are used to replaced your real dependant objects in your sub system. If these real dependant objects have static methods, how might you still go about testing your sub system?
I did find a product that can help you with this, however I guess I was looking for something that might be open source or free. I did find a product that can do this... that was AgileTest.
http://www.polygenix.com
Explaining my point a bit farther.. read my example below...
Example-
You are testing MainClassObject
MainClassObject depends on DependantClassA and DependantClassB.
DependantClassA and DependantClassB both have static methods in them. Using MockObjects, how might you test MainClassObject? Or is there any other testing sub-systems that can help you with this? (Besides AgileTest)
-Dale
[ May 08, 2003: Message edited by: Dale DeMott ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct that you can't mock static methods, as mock objects rely on polymorphic method calls (and static methods aren't polymorphic in Java).
What you can do, is not calling the static method directly, but through a Facade object. The production facade can delegate to the static method, and you can mock it in your tests.
Did that help?
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite sure. Can you explain a bit more about it.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a simple example:
Original code:

New code:
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. That helps!
Dale
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic