JUnit4.8.2原始碼分析-3.2 Computer

來源:互聯網
上載者:User

標籤:style   color   io   使用   ar   java   for   strong   sp   

本系列文章,記錄yqj2065閱讀JUnit原始碼的過程,很多時候在閱讀過程中有許多不理解的地方,例如某某類是幹什麼的,為什麼需要它,為什麼不這樣設計……等等。

org.junit.runner是JUnit最核心的包,其中的Computer/電腦(是這樣翻譯麼),它是個什麼意思呢?剛開始讀JUnitCore的時候,我就很不明白。

package org.junit.runner;import org.junit.runners.Suite;import org.junit.runners.model.InitializationError;import org.junit.runners.model.RunnerBuilder;import org.junit.runner.Runner;/** * Represents a strategy for computing runners and suites. * WARNING: this class is very likely to undergo serious changes in version 4.8 and * beyond. */public class Computer {/** * Returns a new default computer, which runs tests in serial order */public static Computer serial() {return new Computer();}/** * Create a suite for {@code classes}, building Runners with {@code builder}. * Throws an InitializationError if Runner construction fails */public Runner getSuite(final RunnerBuilder builder,Class<?>[] classes) throws InitializationError {return new Suite(new RunnerBuilder() {@Overridepublic Runner runnerForClass(Class<?> testClass) throws Throwable {return getRunner(builder, testClass);}}, classes);}/** * Create a single-class runner for {@code testClass}, using {@code builder} */protected Runner getRunner(RunnerBuilder builder, Class<?> testClass) throws Throwable {return builder.runnerForClass(testClass);}}
其實,如果把它叫作 RunnerFactory,我就會秒懂。

更進一步,如果不考慮它的子類org.junit.experimental.ParallelComputer,可以將Computer的兩個方法定義為static。

多麼標準的靜態工廠。

又一個問題,Computer與org.junit.runners.model.RunnerBuilder為什麼是使用關係而非繼承關係?Computer僅僅作為RunnerBuilder的前台?


總之,在目前階段,我把Computer看成一個工廠。




JUnit4.8.2原始碼分析-3.2 Computer

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.