detecting in a JUnit context

J

JEFF JARRELL

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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

JEFF said:
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.getClassName() looks like a JUnit class
}

Arne
 
J

jeff jarrell

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

Arne Vajhøj said:
JEFF said:
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.getClassName() looks like a JUnit class
}

Arne
 
M

Mark Space

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

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.
 

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,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top