aspose file tools*
The moose likes Testing and the fly likes Help need to write a test junit test case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Help need to write a test junit test case" Watch "Help need to write a test junit test case" New topic
Author

Help need to write a test junit test case

Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108


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.


Thanks and Regards, Pradeep Kumar
SCJP 1.6, SCWCD 5.0
James Dixon
Ranch Hand

Joined: Jun 20, 2009
Posts: 32
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.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Pradeep, can you please be more specific about what your doubt is? With the information you gave, I'd say "just write the test"...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108

Hi all,
Thanks for the reply. That sufficed my requirement.
Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108
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
krishna kanthgaru
Greenhorn

Joined: Feb 12, 2007
Posts: 17
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


no signature ... i am not a celebrity yet
Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108
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

Joined: Feb 12, 2007
Posts: 17
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

Joined: Sep 08, 2006
Posts: 108
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

Joined: Feb 12, 2007
Posts: 17
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

Joined: Sep 08, 2006
Posts: 108
Thanks. even i will look for other alternatives.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help need to write a test junit test case