gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:30:06 27 4
gpt4 key购买 nike

有没有办法让当前测试用例的类执行。通过@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
}

关于java - 获取当前测试用例在 Junit 中运行的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17973844/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com