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

M

mike

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

}

}
 
D

Daniel Pitts

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

Kenneth P. Turvey

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.
 
L

Lew

Kenneth said:
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.

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

mike

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
 

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,991
Messages
2,570,212
Members
46,800
Latest member
Tobi1987

Latest Threads

Top