繁体   English   中英

获取在Junit中运行当前测试用例的类

[英]Get Class in which current test case is running in Junit

有没有办法让当前测试用例的类执行。 通过@Rule批注,我们可以获得当前测试方法的方法名称,但是要获取类,我无法找到获取类的方法。

public void setUp() throws Exception
{
  Method method=MyClass.class.getMethod(testName.getMethodName());

}

我想要获得Myclass,以便可以对包含测试方法的任何类在上述代码中实现概括。

提前致谢。

要获取类名,请使用

@Test
public void setUp() throws Exception
    Class clazz = this.getClass(); //if you want to get Class object
    String name = clazz.getCanonicalName(); //you want to get only class name
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM