Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

JUnit

Thread view: 
akhileshkokardekar@gmail.com - 16 Dec 2006 09:45 GMT
Hi All,

      How i can test a method say X with JUnit which returns void.
This X method internally calls many other methods which are returnig
something.

Thanking You.

Akhilesh
Daniel Dyer - 16 Dec 2006 10:53 GMT
> Hi All,
>
>        How i can test a method say X with JUnit which returns void.
> This X method internally calls many other methods which are returnig
> something.

What does the method do?  Are there pre- and post-conditions (i.e. is  
there some state that you can query before and after invoking it in order  
to determine if it worked properly)?

Dan.

Signature

Daniel Dyer
http://www.uncommons.org

andrewmcdonagh - 16 Dec 2006 13:04 GMT
On Dec 16, 9:45 am, akhileshkokarde...@gmail.com wrote:
> Hi All,
>
[quoted text clipped - 5 lines]
>
> Akhilesh

Test its effect upon the rest of the system.

e.g.

Here's a clock class with Tick method that does not return anything....

import junit.framework.TestCase;

public class ClockTest extends TestCase {

 public void testTickIncrementsTheTime() {
   Clock clock = new Clock(0, 1);

   clock.tick();

   assertEquals("Wrong time!", "0:2", clock.getTime());
 }

 class Clock {
   private int minutes;
   private int seconds;

   public Clock(int mins, int secs) {
     minutes = mins;
     seconds = secs;
   }

   public String getTime() {
     return minutes + ":" + seconds;
   }

   public void tick() {
     seconds++;
   }
 }

}

Andrew
John Ersatznom - 16 Dec 2006 22:11 GMT
> Hi All,
>
>        How i can test a method say X with JUnit which returns void.
> This X method internally calls many other methods which are returnig
> something.

One would assume that it has a side effect. Test for the side effect.
E.g. if the method is a setter, call the getter afterward in your unit
test and check that it behaved appropriately. If it's something more
complicated, it will be trickier to manage, and may even require
checking a property sheet or disk file has been altered appropriately.
Richard Wheeldon - 17 Dec 2006 22:09 GMT
>        How i can test a method say X with JUnit which returns void.
> This X method internally calls many other methods which are returnig
> something.

1. Test pre and post conditions.
2. Take a look at Mock Objects.

Richard


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.