This series of articles record the process of yqj2065 reading the source code of JUnit. Many times there are many things I don't understand in the reading process, such as what a class is doing and why it is needed, why not design it like this ...... And so on.
Org. JUnit. Runner is the core package of JUnit.Computer/computer (is this translation)What does it mean? I didn't understand it when I first started reading 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);}}
In fact, if you call it
RunnerfactoryIn seconds.
Furthermore, if you do not consider its subclass org. JUnit.Experimental. parallelcomputerYou can define the two methods of the computer as static.
What a standard static factory is.
Another question is, why is the relationship between the computer and org. JUnit. Runners. model. runnerbuilder used rather than the inheritance relationship? Is the computer only used as the front-end of runnerbuilder?
In short, in the current stage, I think of computer as a factory.
Junit4.8.2 source code analysis-3.2 computer