• 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

Help need to write a test junit test case

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


Hi ranchers,

I have a requirement wherein in need to test a overridden method which calls super.execute() at the end of the method and from there on the control goes inside the application. How should i write a test program for this. For better understanding i will write the method which needs to be tested. here




Can someone help me to write a test case for this scenario.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pradeep

I guess the problem you are having is that you want to test the code in your app, but then don't want to run through to the super method, which probably has other dependencies.

One thing you could try doing is substituting the SuperClass class with your own.
This isn't too difficult if the super class is in a seperate jar file. You then just create a class with the same methods in the same package, and include that in your test build rather than the actual SuperClass. When you then run through your unit test all the code that you want to exercise is, and when it does call super.execute() the behaviour can be controlled by you.

Hopefully this should make some sense, but for more ideas about testing this kind of code I cannot recommend 'Working Effectively with Legacy Code' enough. As the title suggests it talks about working with and putting tests in place around the most impenetrable of code.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pradeep, can you please be more specific about what your doubt is? With the information you gave, I'd say "just write the test"...
 
Pradeep Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi all,
Thanks for the reply. That sufficed my requirement.
 
Pradeep Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

To be very precise of the context I have to test on of my application class which calls super.execute() as the last statement of the method. The super class is in another jar file and the super class execute method has got lot of dependencies. My intention is to test the execute method of the application and not to test the super class which is in another jar file.

Ex:


If i write a FrameworkClassInAnotherJar class in my application as mentioned in the post earlier the call will definitly land in the dummy super class i coded and suffice for my test case but when i do a build and run my application will it wont land the dummy class? How to go about this. Kindly provide me an optimum solution for testing my application class execute method.

Thanks and Regards,
Pradeep
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whats the benefit you are getting out of extending the super class?

if its code resuse , the super class should really be a member of your class and not parent class.

public class MyApp extends {

SuperClass member = //initialize

// method to be tested using JUnit.
execute()
{
//only validate and setSomeVariables methods should be tested.
validate();
setSomeVariables();

// calls the super class execute method which is present in another jar file
//and i am not interested in testing the super class execute method.
member.execute();
}
}

of course , i am assuming this is the only use you have with super class and

Disclaimer: there might be better solutions :-)

Pradeep Kumar wrote:Hi,

To be very precise of the context I have to test on of my application class which calls super.execute() as the last statement of the method. The super class is in another jar file and the super class execute method has got lot of dependencies. My intention is to test the execute method of the application and not to test the super class which is in another jar file.

Ex:


If i write a FrameworkClassInAnotherJar class in my application as mentioned in the post earlier the call will definitly land in the dummy super class i coded and suffice for my test case but when i do a build and run my application will it wont land the dummy class? How to go about this. Kindly provide me an optimum solution for testing my application class execute method.

Thanks and Regards,
Pradeep

 
Pradeep Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am extending the super class not by choice. The issues is that i am using a framework which gives a default behavior. So all the applications developed using the framework should extend the class from the framework and override the execute method to get the custom behavior for that specific application. The point here is the control is totally governed by the framework and gives the applications hook points to plug in for extensions for custom behavior specific to applications developed based on the framework. Hope the requirement is clear from my end.
 
krishna kanthgaru
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pretty clear pradeep.

why don't you extract your class logic into another method and call the new method from execute and write a test case for that new method?
for extensions for custom behavior specific to applications developed based on the framework. Hope the requirement is clear from my end.
 
Pradeep Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed and indeed a good choice. But for me its only partial answer as i would jott down your approach.



Now if we test the doSubClassSpecificOperation method it will suffice my need. But there are few questions which still have to be answered.

1. What if the Class is a legacy class and to just write a test case we are modifying the code.
2. Also when we test doSubClassSpecificOperation we are only testing that method but the execute method is still not tested for super.execute() method call statement.

Thanks and Regards,
Pradeep
 
krishna kanthgaru
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. What if the Class is a legacy class and to just write a test case we are modifying the code.
agree , risks come with the benefits
2. Also when we test doSubClassSpecificOperation we are only testing that method but the execute method is still not tested for super.execute() method call statement.
you may want to mock. not sure if there is a mocking framework that would mock a overridden method :-) , will try around and let you know.
 
Pradeep Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. even i will look for other alternatives.
reply
    Bookmark Topic Watch Topic
  • New Topic