Design for test

Discussion in 'Java' started by mike, Mar 6, 2008.

  1. mike

    mike Guest

    Hi,

    Today we have a class that don't have any tests. We need to add unit
    tests for it so I imagine I have to make an interface for MySession
    class. How can I handle the static one since an interface( called
    ISession ) can only contain public or abstract modifier?

    cheers,

    //mike

    Class that we need to abstract to an interface:
    =================================
    public class MySession {

    public static CallSession createFrom(SipFactory sipFactory,
    SipServletRequest initialInvite,
    Address address) throws IOException, ServletParseException,
    ServletException {

    .......

    return (CallSession)callsession;

    }



    public String getId() {
    return getSession().getId();
    }

    }
     
    mike, Mar 6, 2008
    #1
    1. Advertising

  2. mike

    Mark Space Guest

    mike wrote:
    > Hi,
    >
    > Today we have a class that don't have any tests. We need to add unit
    > tests for it so I imagine I have to make an interface for MySession
    > class. How can I handle the static one since an interface( called
    > ISession ) can only contain public or abstract modifier?


    How about an abstract class instead of an interface?

    But basically you are correct, it's a bit of a mess-up that Interfaces
    in Java are so limited. An abstract class will do the same thing
    (mostly) and can have more stuff besides.
     
    Mark Space, Mar 6, 2008
    #2
    1. Advertising

  3. mike

    Mark Space Guest

    Mark Space wrote:
    > mike wrote:
    >> Hi,
    >>
    >> Today we have a class that don't have any tests. We need to add unit
    >> tests for it so I imagine I have to make an interface for MySession
    >> class. How can I handle the static one since an interface( called
    >> ISession ) can only contain public or abstract modifier?

    >
    > How about an abstract class instead of an interface?
    >
    > But basically you are correct, it's a bit of a mess-up that Interfaces
    > in Java are so limited. An abstract class will do the same thing
    > (mostly) and can have more stuff besides.


    Hmm, thinking about this a bit more, this might be kind of dangerous.

    Static methods don't "vitualize" with class type. If you have a class
    MyClass which extends MyAbstractClass both with a static method
    getInt(), then

    MyAbstactClass c = new MyClass();
    c.getInt();

    Is always going to access the implementation in MyAbstractClass, which I
    doubt is what you want, if you've also declared getInt() in MyClass.

    And I think this is why there are no static methods in Java interfaces.
    With no implementation for the static method allow in the interface,
    it really doesn't make sense for a static method to be there.

    It might be best to just bite the bullet and implement a specific test
    just for that class. JUnit and a good IDE will save you some typing
    here. That may be the best you can do.
     
    Mark Space, Mar 6, 2008
    #3
  4. mike

    Guest

    On 6 Mar, 15:28, mike <> wrote:
    > Hi,
    >
    > Today we have a class that don't have any tests. We need to add unit
    > tests for it so I imagine I have to make an interface for MySession
    > class. How can I handle the static one since an interface( called
    > ISession ) can only contain public or abstract modifier?


    There's nothing stopping you from writing a MySessionTest that tests
    the
    class directly. In fact, this is what you want to do. However, since
    the
    test object (MySession) has dependecies towards SipFactory and
    SipServletRequest
    you may want to to create stubs for these so that you can verify that
    MySession is using them correctly. Nowadays you don't have to create
    the
    stubs manually but can instead use tools like EasyMock (Google for
    it).

    So with stubs for SipFactory, SipServletRequest instantiated you can
    just
    call MySession.createFrom() and examine the contents of CallSession to
    verify that it is correct given the input to createFrom().

    Regards,
    Greger
     
    , Mar 7, 2008
    #4
    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. Sting
    Replies:
    5
    Views:
    1,630
    Natty Gur
    Jun 8, 2004
  2. Robert Allan Schwartz
    Replies:
    7
    Views:
    373
    Marco Manfredini
    Aug 13, 2004
  3. Chris
    Replies:
    1
    Views:
    669
    Chris
    May 11, 2006
  4. Edvard Majakari
    Replies:
    4
    Views:
    687
    Edvard Majakari
    Feb 25, 2005
  5. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    707
    Skybuck Flying
    Oct 4, 2009
Loading...

Share This Page