simplysiby
simplysiby

Reputation: 584

How to find out if a method call originated from Test Class

Hii... Is there any way to find out if the method call originated from a test class? If its from a test class... then I need to initialize some dummy values for the variables in the class . I would like to write the Test Class with minimal change in the source code... The class is following a singleton pattern..So its private constructor gets called which is calling some code which is blocking my testing. So I need to call my dummy methods from within in the private constructor so that it works smoothly..

Currently I am doing this...

StackTraceElement[] stack = new Throwable().getStackTrace();
boolean blnFrmTesting = false;
for (StackTraceElement stackTraceElement : stack) {
    if(null != stackTraceElement && null != stackTraceElement.getFileName() && stackTraceElement.getFileName().endsWith("Test.java")) {
        blnFrmTesting = true;
        break;
    }
}
return blnFrmTesting;

Is this a correct method...Or is there any other way.. like checking annotation...("@Test")

Upvotes: 0

Views: 807

Answers (3)

AlexR
AlexR

Reputation: 115378

I generally agree with Sudhir, and do not really understand what did Riduidel want to say recommending to use Mocking. Mocking is fine if you wish to simulate the class' environment and neighborhood.

I think that your method is fine. You can really improve it if you add check of @Test annotation and a fact that the class extends TestCase. If you add support JUniot and testNG you can even publish your code and probably other people can use it.

But I think that probably you can even simplify the method. I used special system property for this purpose. Typically I had to identify that the code is running under application server, so I used property typical for application server. For example jboss.server.name for JBoss and catalina.base for Tomcat. If JUnit does not create any special property you can do it yourself in the beginning of test and check in code.

Upvotes: 0

Sudhir Jonathan
Sudhir Jonathan

Reputation: 17526

Irrespective of whether you find an answer to this question or not, it might make more sense not to perform this check in the first place, and structure your code accordingly... code which does something else when called by a test isn't really tested, is it?

You could also take a look at using a DI pattern and framework like Guice or Spring... that would make things a lot easier to test, while probably resulting in less and simpler code.

Upvotes: 3

Riduidel
Riduidel

Reputation: 22300

Well, for the technical part, I suggest you should instead try to see if class name contains Test, instaead of file name, which is (although Java specification tries to normalize it) always a little more fuzzy (think about inner class, as an example).

However, in a more general fashion, your code seems to ignore roughly ten years of Java engineering by ignoring the existence of test frameworks (JUniot, TestNG) and their associated ecosystem. Particularly, to define "dummy values", the domain of mocking frameworks is the way to go. There are currently quite a few interesting alternatives :

Obviously, they may interfere with your singleton (or not). However, I must tell you that with the davent of IoC frameworks, the singleton pattern is now generally considered to be deprecated.

Upvotes: 3

Related Questions