Can code detect if it is running under JUnit?
1 posts in topic
Flat View  Flat View

Posted By:   Mark_Meuer
Posted On:   Tuesday, October 28, 2003 11:31 AM

Is there a way for code being run by JUnit to detect the fact that a JUnit test is in progress? We have some error-handling code that really needs to behave differently under test than when it is run as part of our application.

Ideally, I was hoping to find a system property that is set by all of the test runners that indicate a test is under way. I didn't see anything about this in the documentation.

Any help you can give would be appreciated.


Re: Can code detect if it is running under JUnit?

Posted By:   Anonymous  
Posted On:   Tuesday, October 28, 2003 10:54 PM

You have a coupling problem here - if your code under test needs to know who called it, you are violating the Open-Closed Principle.

What you should do is: factor your error handling code into its own class and extract an interface for it. Let the clients pass an error handler to your code under test. That way, your unit tests can simply pass a different error handler than your production code. You could even use Mock Objects to tests wether the error handling is done correctly.

Hope this helps.

About | Sitemap | Contact