如何知道当前触发了哪个测试

时间:2018-10-18 10:36:50

标签: java junit

我有下一个代码

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测试,或者如果有多个子类,则如何知道呢?

1 个答案:

答案 0 :(得分:1)

您可以通过以下方式获取启动父类创建的后代类的名称

// in the constructor of the parent System.out.println(Thread.currentThread().getStackTrace()[2].getClassName());

在具有多个继承级别的情况下,您可以编写一些额外的自定义逻辑并遍历当前的堆栈跟踪。

免责声明:您要做的事情看起来并不好。使用@Before@BeforeClass之类的内置功能或一些重构可能是更好的解决方案。

相关问题