Testing class with no methods with junit

Discussion in 'Java' started by andrewzzz, Feb 26, 2007.

  1. andrewzzz

    andrewzzz Guest

    Hi guys,
    I'm quite new with Junit...I have to test a full package..but I really
    don't know how to test a class that only has constructor....and no
    other methods..
    How do I do that?
    Thank you guys for your support.
    (I'm using eclipse)
     
    andrewzzz, Feb 26, 2007
    #1
    1. Advertising

  2. andrewzzz

    Daniel Dyer Guest

    On Mon, 26 Feb 2007 22:11:49 -0000, andrewzzz <> wrote:

    > Hi guys,
    > I'm quite new with Junit...I have to test a full package..but I really
    > don't know how to test a class that only has constructor....and no
    > other methods..
    > How do I do that?
    > Thank you guys for your support.
    > (I'm using eclipse)


    What does this class do? If it really has no public interface (not even
    accessible fields?), it presumably has no observable behaviour and
    therefore there isn't much you can do as far as testing goes. It doesn't
    sound like a very useful class. While I'm not entirely sold on
    test-driven design, often a class that can't easily be tested is symptom
    of a poor design.

    Dan.

    --
    Daniel Dyer
    https://watchmaker.dev.java.net - Evolutionary Algorithm Framework for Java
     
    Daniel Dyer, Feb 26, 2007
    #2
    1. Advertising

  3. "andrewzzz" <> writes:

    F> Hi guys,
    > I'm quite new with Junit...I have to test a full package..but I really
    > don't know how to test a class that only has constructor....and no
    > other methods..
    > How do I do that?


    You test behavior. If a class has only a constructor,
    (and it's not just a data container), all the behavior
    must be in the constructor. Test it.

    (It seems very likely, by the way, that you'd be better
    off re-considering the design, or throwing out the whole
    mess and trying it test-first, to see if you can find
    a more testable design. But I'm going to assume you're
    stuck.)

    (In this example, I've assumed that there aren't any
    methods, but there is accessible data in the instance
    fields. It may be that you have something really bizarre,
    like a constructor that you only call for the side-effect.
    In that case, you could test the side effect, but it's
    even more likely than before that you're having trouble
    because you're trying to do something horribly, horribly
    Wrong.)



    class Time {
    public int hour;
    public int minute;
    public int seconds;

    Time (int secondsSinceMidnight) { ... }
    }


    class TimeTest extends junit.framework.TestCase {

    public void testOffsetIsFromMidnight() {
    Time t = new Time(0);
    assertEquals(0, t.hour);
    assertEquals(0, t.minute);
    assertEquals(0, t.second);
    }

    public void testComputeHoursMinutesSecondsFromOffset() {
    Time t = new Time(13*3600 + 46*60+17);
    assertEquals(13, t.hour);
    assertEquals(46, t.minute);
    assertEquals(17, t.second);
    }

    public void testRollover() {
    Time t = new Time(27*3600);
    assertEquals(3, t.hour);
    }

    public void testRejectNegativeOffset() {
    try {
    Time t = new Time(-1);
    } catch (Exception e) {}

    fail("Invalid argument.);
    }
    }


    (That was kindof fun. Everyone has my explicit permission
    to use this amazing TestSuite for any purpose at all, up
    to and including total world domination.)

    --
    Mark Jeffcoat
    Austin, TX
     
    Mark Jeffcoat, Feb 27, 2007
    #3
    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. Roger
    Replies:
    0
    Views:
    480
    Roger
    Sep 1, 2004
  2. Rhino
    Replies:
    4
    Views:
    5,786
    Roedy Green
    Jan 13, 2006
  3. Robert M. Gary
    Replies:
    1
    Views:
    1,534
    Jim Korman
    Dec 29, 2006
  4. Replies:
    1
    Views:
    641
    Chris
    May 9, 2007
  5. Kenneth McDonald
    Replies:
    5
    Views:
    387
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page