栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在测试套件中定义JUnit方法规则?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在测试套件中定义JUnit方法规则?

可以做到,但是需要一些工作。您需要定义自己的Suite运行程序和测试运行程序,然后在测试运行程序中覆盖runChild()。使用以下内容:

AllTests.java:

@RunWith(MySuite.class)@SuiteClasses({Class1Test.class})public class AllTests {}

Class1Test.java:

public class Class1Test {    @Deprecated @Test public void test1() {        System.out.println("" + this.getClass().getName() + " test1");    }    @Test public void test2() {        System.out.println("" + this.getClass().getName() + " test2");    }}

请注意,我注释

test1()
@Deprecated
。当您
@Deprecated
在测试中具有注释时,您想做一些不同的事情,因此我们需要扩展Suite以使用自定义
Runner

public class MySuite extends Suite {    // copied from Suite    private static Class<?>[] getAnnotatedClasses(Class<?> klass) throws InitializationError {        Suite.SuiteClasses annotation = klass.getAnnotation(Suite.SuiteClasses.class);        if (annotation == null) { throw new InitializationError(String.format("class '%s' must have a SuiteClasses annotation", klass.getName()));        }        return annotation.value();    }    // copied from Suite    public MySuite(Class<?> klass, RunnerBuilder builder) throws InitializationError {        super(null, getRunners(getAnnotatedClasses(klass)));    }    public static List<Runner> getRunners(Class<?>[] classes) throws InitializationError {        List<Runner> runners = new linkedList<Runner>();        for (Class<?> klazz : classes) { runners.add(new MyRunner(klazz));        }        return runners;    }}

JUnit

Runner
为将运行的每个测试创建一个。通常,Suite只会创建默认值
BlockJUnit4ClassRunner
,我们在这里所做的就是重写Suite的构造函数,该构造函数从
SuiteClass
批注中读取类,然后使用它们创建自己的运行程序
MyRunner
。这是我们的MyRunner类:

public class MyRunner extends BlockJUnit4ClassRunner {    public MyRunner(Class<?> klass) throws InitializationError {        super(klass);    }    @Override    protected void runChild(final frameworkMethod method, RunNotifier notifier) {        Description description= describeChild(method);        if (method.getAnnotation(Ignore.class) != null) { notifier.fireTestIgnored(description);        } else { if (description.getAnnotation(Deprecated.class) != null) {     System.out.println("name=" + description.getMethodName() + " annotations=" + description.getAnnotations()); } runLeaf(methodBlock(method), description, notifier);        }    }}

大部分是从复制

BlockJUnit4ClassRunner
。我添加的位是:

if (description.getAnnotation(Deprecated.class) != null) {    System.out.println("name=" + description.getMethodName() + " annotations=" + description.getAnnotations());}

在这里我们测试

@Deprecated
方法上注释的存在,如果存在,请执行一些操作。剩下的作为练习留给读者。当我运行上面的套件时,我得到的输出是:

name=test1 annotations=[@java.lang.Deprecated(), @org.junit.Test(expected=class org.junit.Test$None, timeout=0)]uk.co.farwell.junit.run.Class1Test test1uk.co.farwell.junit.run.Class1Test test2

请注意,Suite具有多个构造函数,具体取决于调用方式。以上内容适用于Eclipse,但我尚未测试运行套件的其他方式。有关更多信息,请参见Suite的各种构造函数旁边的注释。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569236.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号