如何查明方法调用是否源自测试类

发布于 2024-10-03 16:48:14 字数 631 浏览 2 评论 0原文

嗨... 有什么方法可以查明方法调用是否源自测试类? 如果它来自测试类...那么我需要为类中的变量初始化一些虚拟值。我想编写测试类,对源代码进行最小的更改...... 该类遵循单例模式。因此它的私有构造函数被调用,该构造函数正在调用一些阻止我的测试的代码。所以我需要从私有构造函数中调用我的虚拟方法,以便它顺利工作..

目前我正在这样做...

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;

这是一个正确的方法...还是有其他方法..比如检查注释.. 。(“@测试”)

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")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

累赘 2024-10-10 16:48:14

好吧,对于技术部分,我建议您应该尝试查看 类名 是否包含 Test,而不是文件名,这是(尽管 Java 规范试图对其进行规范化)总是有点模糊(以内部类为例)。

然而,从更一般的角度来看,您的代码似乎忽略了测试框架(JUniot、TestNG)及其相关生态系统的存在,从而忽略了大约十年的 Java 工程。特别是,要定义“虚拟值”,模拟框架领域是最佳选择。目前有很多有趣的替代方案:

显然,它们可能会干扰您的单例(或不会)。然而,我必须告诉你,随着 IoC 框架的出现,单例模式现在通常被认为已被弃用。

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.

小镇女孩 2024-10-10 16:48:14

无论您是否找到这个问题的答案,首先不执行此检查并相应地构造您的代码可能更有意义……在测试调用时执行其他操作的代码实际上并不是这样测试过了,是吗?

您还可以考虑使用 DI 模式和框架,例如 GuiceSpring...这将使测试变得更加容易,同时可能会导致代码更少、更简单。

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.

烂柯人 2024-10-10 16:48:14

我总体上同意 Sudhir 的观点,但不太明白 Riduidel 想要推荐使用 Mocking 的意思。如果您想模拟班级环境和邻居,则可以使用模拟。

我觉得你的方法很好。如果添加 @Test 注释的检查以及该类扩展 TestCase 的事实,您确实可以改进它。如果您添加对 JUniot 和 testNG 的支持,您甚至可以发布您的代码,并且其他人可能可以使用它。

但我认为你甚至可以简化该方法。为此,我使用了特殊的系统属性。通常,我必须确定代码正在应用程序服务器下运行,因此我使用应用程序服务器的典型属性。例如 JBoss 的 jboss.server.name 和 Tomcat 的 catalina.base。如果 JUnit 没有创建任何特殊属性,您可以在测试开始时自己创建并签入代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文