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

Tip: Looking for answers? Try searching our database.

detecting in a JUnit context

Thread view: 
JEFF JARRELL - 03 Sep 2007 02:46 GMT
How can I tell if I am running in a JUNIT context?  I have some code that
needs to behave differently if it is running in an appserver.

jeff
Arne Vajhøj - 03 Sep 2007 03:06 GMT
> How can I tell if I am running in a JUNIT context?  I have some code that
> needs to behave differently if it is running in an appserver.

Somehow I have a strong dislike for code that tests for and
change behavior when run during unit test.

It reduces the value of unit testing significantly.

So I think you should redesign.

If you insist then try:

      Exception ex = new Exception();
      StackTraceElement[] ste = ex.getStackTrace();
      for(int i = 0; i < ste.length; i++) {
         // test if ste[i].getClassName() looks like a JUnit class
      }

Arne
jeff jarrell - 03 Sep 2007 03:49 GMT
This use isn't pervasive. It is in a Spring bootstrapper.  To find some
global config files I use a Spring ClassPathResource to get to them.  In
eclipse, I can just add a ClassFolder to LinkToAFolderInTheFileSystem.  In
JUnit this is fine. But when I go to the AppServer context, I don't have the
GlobalClassPath Link to something in the file system thing available. I move
the config files around to deploy in the web-app and I need to adjust the
classPathReference accordingly.

If you have another thought here, I am all ears.

Your solution for JUnit detection should work fine.  Thanks.

jeff

>> How can I tell if I am running in a JUNIT context?  I have some code that
>> needs to behave differently if it is running in an appserver.
[quoted text clipped - 15 lines]
>
> Arne
Mark Space - 05 Sep 2007 05:30 GMT
> This use isn't pervasive. It is in a Spring bootstrapper.  To find some
> global config files I use a Spring ClassPathResource to get to them.  In
[quoted text clipped - 3 lines]
> the config files around to deploy in the web-app and I need to adjust the
> classPathReference accordingly.

Pass in whatever context you need to do your work in an
environment-independent manner?

For testing, just pass in null instead of a regular context.  Then
instead of testing for JUint being on the stack someplace, just test for
the context object being null.


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.