You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Erik Price <ep...@ptc.com> on 2003/05/21 16:41:10 UTC
[OT] unit testing something that uses JNDI
Hi,
I would like to write some unit tests for some classes. Unfortunately
the classes use some JNDI data so they need to be run from within my app
server (Tomcat in this case) in order to get the JNDI data from the
web.xml. Any suggestions on a good way to go about this?
Thank you,
Erik
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
SOLVED Re: [OT] unit testing something that uses JNDI
Posted by Erik Price <ep...@ptc.com>.
Erik Price wrote:
> Hi,
>
> I would like to write some unit tests for some classes. Unfortunately
> the classes use some JNDI data so they need to be run from within my app
> server (Tomcat in this case) in order to get the JNDI data from the
> web.xml. Any suggestions on a good way to go about this?
The solution is to use mock objects, though it will require a small
refactoring made to the original code:
If I move the code that calls JNDI to its own method (called
"getJndiData" or something), then I can write an anonymous subclass of
my class in the unit test and override this method to return a
predetermined (non-JNDI) mock value to use in the unit test. This
obviously doesn't do anything to assure that the JNDI call works, but it
means I can write my unit test now instead of waiting until my webapp is
at a point where it is assembled "enough" to deploy and test in the
container.
Someone else recommended Cactus, it is on my to-do list to learn how to
use it but for right now I can use JUnit + the mock object.
Just thought I'd share this cool method.
Erik
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org