java - 如何知道当前触发了哪个测试
问题描述
我有下一个代码
class Test extend Parent{
@Test
public void testMethod(){
};
}
class Parent {
'here I need to have some check which test is running now'
}
至于在Test类之前初始化Parent类,如何知道现在正在运行哪个jUnit测试或者如果有多个子类是哪个子类?
解决方案
您可以通过以下方式获取启动父类创建的后代类的名称:
// in the constructor of the parent
System.out.println(Thread.currentThread().getStackTrace()[2].getClassName());
在多继承级别的情况下,您可以编写一些额外的自定义逻辑并遍历当前堆栈跟踪。
免责声明:您将要做的事情看起来并不好。使用内置功能,如@Before
,@BeforeClass
或进行一些重构可能是更好的解决方案。
推荐阅读
- apache - 具有两个或多个字段的 AEM OSGi 配置多重属性
- excel - 使用 Acumatica 的导出方法为 Excel 单元格填充颜色
- java - 如何在Java中连接两个列表中的单词
- elixir - 在我的 elixir/phoenix 项目中执行操作后如何在 aws cloudfront 中创建失效
- java - 如何在每个 @Test 之前运行 @BeforeClass 方法
- java - 使用 Hibernate 从类中获取 oneToMany 字段
- c++ - Z- PURE WIN32 应用程序中的排序
- python - 漂亮地打印一个二叉搜索树
- git - git客户端之间的兼容性
- objective-c - ARC 在 ios 中的以下代码片段中如何工作?