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 / March 2008

Tip: Looking for answers? Try searching our database.

Design for test

Thread view: 
mike - 06 Mar 2008 14:28 GMT
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();
    }

}
Mark Space - 06 Mar 2008 20:17 GMT
> 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 - 06 Mar 2008 21:14 GMT
>> Hi,
>>
[quoted text clipped - 8 lines]
> 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.
greger.olsson@redstone.se - 07 Mar 2008 10:09 GMT
> 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


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.