JUnit

Discussion in 'Java' started by akhileshkokardekar@gmail.com, Dec 16, 2006.

  1. Guest

    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
    , Dec 16, 2006
    #1
    1. Advertising

  2. Daniel Dyer Guest

    On Sat, 16 Dec 2006 09:45:01 -0000, <> wrote:

    > 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.

    --
    Daniel Dyer
    http://www.uncommons.org
    Daniel Dyer, Dec 16, 2006
    #2
    1. Advertising

  3. On Dec 16, 9:45 am, wrote:
    > 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


    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
    andrewmcdonagh, Dec 16, 2006
    #3
  4. wrote:
    > 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.
    John Ersatznom, Dec 16, 2006
    #4
  5. wrote:
    > 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
    Richard Wheeldon, Dec 17, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Koenning
    Replies:
    0
    Views:
    1,433
    Tom Koenning
    Jul 28, 2003
  2. Scout
    Replies:
    0
    Views:
    519
    Scout
    Aug 4, 2003
  3. Christopher Blunck

    Re: Junit Ant Task

    Christopher Blunck, Aug 13, 2003, in forum: Java
    Replies:
    1
    Views:
    596
    Scout
    Aug 19, 2003
  4. Jan
    Replies:
    0
    Views:
    3,712
  5. Stian Bakken

    eclipse, junit analyzer

    Stian Bakken, Nov 7, 2003, in forum: Java
    Replies:
    1
    Views:
    595
    Alain Javier Guarnieri del Gesu
    Nov 8, 2003
Loading...

Share This Page