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 / November 2007

Tip: Looking for answers? Try searching our database.

How to test a class that is started via a Servlet.

Thread view: 
mike - 31 Oct 2007 10:36 GMT
Hi,

I have the following class MessageManager ( which sends messages in
different formats).
In weblogic 8.1 I use a servelet to call start() and stop() to
activate/deactivate my MessageManager.

How can I activate the 'public void onRecMsg(String msg)'-method when
server is up and running?
Can I use a junit test  and execute it in runtime or do I need a
Servlet that calls this method with a message?

Appreciate your advice.

cheers,

//mikael

MessageManager
=============
public class MessageManager implements MessageInterface {
    private static MessageQueue msgSession = null;
    private int msgFormat = MessageTypes.XML;
    private static Logger LOG = Logger.getLogger(MessageManager.class);

    public MessageManager(int msgFormat) {
      this.msgFormat = msgFormat;
    }

    public void start() {
      try {
             if (jmsSession == null) {
                msgSession = new MessageQueue();
                msgSession.create();
                msgSession.setReceiver(McxJndiName.JMS_MSG_QUEUE_NAME,
this);
          }

            switch (msgFormat) {
            case MessageTypes.XML:
                sender = new XMLSender();
                break;
            case MessageTypes.TXT:
                sender = new TXTSender();
                break;
            default:
                break;
            }

        } catch (Exception ex) {
            LOG.error("Failed to start " + ex);
            stop();
        }

    }

    public void stop() {
    try {
        if (msgSession != null)
           msgSession.close();
        } catch (Exception ex) {
            LOG.error("Could not close message session" + ex);
        }

    sender.close();
    }

    public void onRecMsg(String msg) {
      sender.send(msg);

    }

}
Daniel Pitts - 31 Oct 2007 18:06 GMT
> Hi,
>
[quoted text clipped - 7 lines]
> Can I use a junit test  and execute it in runtime or do I need a
> Servlet that calls this method with a message?

JUnit is for testing, and should NOT be used in production code to do
anything.  By activating, do you mean invoking/calling?  You could using
logging to show that its happening, or you could just assume that it
works because you've tested it appropriately.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Kenneth P. Turvey - 31 Oct 2007 20:57 GMT
> JUnit is for testing, and should NOT be used in production code to do
> anything.  By activating, do you mean invoking/calling?  You could using
> logging to show that its happening, or you could just assume that it
> works because you've tested it appropriately.

I'm not sure I understood the initial question.  

I'm having a problem testing some servlet code using JUnit as well and he
might be having the same issue.  In my code I've had to separate the code
that uses the HttpServletRequest and HttpServletResponse classes to a
separate class, that remains untested, because JUnit always caused an
error when trying to test the class that contained these methods.  

It would be nice to be able to test the servlet code as well.  In my case
these are just thin wrappers around code that is tested, but it really
shouldn't have to be.  

Signature

Kenneth P. Turvey <kt-usenet@squeakydolphin.com>

Lew - 01 Nov 2007 00:10 GMT
> I'm not sure I understood the initial question.  
>
[quoted text clipped - 7 lines]
> these are just thin wrappers around code that is tested, but it really
> shouldn't have to be.  

<http://jakarta.apache.org/cactus/>

Signature

Lew

mike - 01 Nov 2007 08:49 GMT
On 31 Okt, 18:06, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
> > Hi,
>
[quoted text clipped - 10 lines]
> JUnit is for testing, and should NOT be used in production code to do
> anything.

 I am looking for something that is similair to junit. I cannot unit
test my stuff since I have
 problems to stub MBeanServer and I did not think it was necessary
since I really only need something
 that invokes my onMsgRec(String msg)-method in runtime.

By activating, do you mean invoking/calling?

Today a servlet with init() and destroy() automatically called by WLS
at start and stop calls my start() and stop() in
MessageManager. Maybe I can add a doGet() that calls onMsgRec(String
msg)-method?

You could using
> logging to show that its happening, or you could just assume that it
> works because you've tested it appropriately.

I have to trigger onMsgRec(String msg) since this is called when the
system finds a problem ( not very often).
Since a customer will be looking at the solution I must be able to
trigger the condition.

cheers,

//mikael

> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


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.