【问题标题】:Junit categories - which category I am in?Junit 类别 - 我属于哪个类别?
【发布时间】:2020-11-15 17:57:15
【问题描述】:

我正在使用 JUNIT 的 @categories 并想在一个方法中检查我所在的类别。

例如 if (category.name == "sanity") //做点什么

有没有办法做到这一点? 我想避免将参数传递给此方法,因为我在项目中有超过 800 次调用它

【问题讨论】:

    标签: java junit automation categories


    【解决方案1】:

    我相信您可以使用与确定任何其他类是否具有特定注释及其值相同的方法来执行此操作 - 使用 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 2017-06-16
      相关资源
      最近更新 更多