Junit 类别 - 我属于哪个类别?
Junit categories - which category I am in?
我正在使用 JUNIT 的 @categories 并想检查我属于哪个类别的方法。
例如
如果(category.name ==“理智”)
//做点什么
有什么办法吗?
我想避免必须将参数传递给此方法,因为我在项目中调用了 800 多次
我相信你可以用同样的方法来确定其他 class 是否有特定的注释及其值 - 使用 Java 反射机制。
作为您的特定案例的快速示例,您可以像这样:
@Category(Sanity.class)
public class MyTest {
@Test
public void testWhatever() {
if (isOfCategory(Sanity.class)) {
// specific actions needed for any tests that falls into Sanity category:
System.out.println("Running Sanity Test");
}
// test whatever you need...
}
private boolean isOfCategory(Class<?> categoryClass) {
Class<? extends MyTest> thisClass = getClass();
if (thisClass.isAnnotationPresent(Category.class)) {
Category category = thisClass.getAnnotation(Category.class);
List<Class<?>> values = Arrays.asList(category.value());
return values.contains(categoryClass);
}
return false;
}
}
我正在使用 JUNIT 的 @categories 并想检查我属于哪个类别的方法。
例如 如果(category.name ==“理智”) //做点什么
有什么办法吗? 我想避免必须将参数传递给此方法,因为我在项目中调用了 800 多次
我相信你可以用同样的方法来确定其他 class 是否有特定的注释及其值 - 使用 Java 反射机制。
作为您的特定案例的快速示例,您可以像这样:
@Category(Sanity.class)
public class MyTest {
@Test
public void testWhatever() {
if (isOfCategory(Sanity.class)) {
// specific actions needed for any tests that falls into Sanity category:
System.out.println("Running Sanity Test");
}
// test whatever you need...
}
private boolean isOfCategory(Class<?> categoryClass) {
Class<? extends MyTest> thisClass = getClass();
if (thisClass.isAnnotationPresent(Category.class)) {
Category category = thisClass.getAnnotation(Category.class);
List<Class<?>> values = Arrays.asList(category.value());
return values.contains(categoryClass);
}
return false;
}
}