Design for test

M

mike

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();
}

}
 
M

Mark Space

mike said:
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.
 
M

Mark Space

Mark said:
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.
 
G

greger.olsson

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top