@Runwith

作者: holiday170309 | 来源:发表于2017-06-04 11:56 被阅读0次

概念

testRunner : 运行测试类的类

@Runwith : 告诉junit的框架应该是使用哪个testRunner

testRunner类关系

自定义一个简单的testRunner的方法是继承junit默认的runner BlockJUnit4ClassRunner。

public class BlockJUnit4ClassRunner extends ParentRunner<FrameworkMethod> {
 
  @Override
  protected List<FrameworkMethod> getChildren() {
    // scan test class for methonds annotated with @Test
  }
 
  @Override
  protected Description describeChild(FrameworkMethod method) {
    // create Description based on method name
  }
 
  @Override
  protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
    if (/* method not annotated with @Ignore */) {
      // run methods annotated with @Before
      // run test method
      // run methods annotated with @After
    }
  }
}

简单的自定义:在没给测试之前打印日志。

public class MyTestRunner extends BlockJUnit4ClassRunner {
    /**
     * Creates a BlockJUnit4ClassRunner to run {@code klass}
     *
     * @param klass
     * @throws InitializationError if the test class is malformed.
     */
    public MyTestRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
        System.out.println("this is my cusstom test runner : " + method.toString());

        super.runChild(method, notifier);
    }
}

引用

  1. Understanding JUnit's Runner architecture

相关文章

网友评论

      本文标题:@Runwith

      本文链接:https://www.haomeiwen.com/subject/aruvfxtx.html