【问题标题】:Get Class in which current test case is running in Junit获取当前测试用例在 Junit 中运行的类
【发布时间】:2013-07-31 23:07:49
【问题描述】:

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

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

}

我想获取 Myclass 以便我可以在上面的代码中为任何包含测试方法的类实现泛化。

提前致谢。

【问题讨论】:

    标签: java junit4


    【解决方案1】:

    获取类名使用

    @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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      • 2013-11-17
      • 2015-06-28
      • 2011-12-09
      • 2019-04-18
      • 2017-08-09
      相关资源
      最近更新 更多