Article From:https://www.cnblogs.com/linkworld/p/9061967.html

1. Statement

  • There is only one way to abstract class Statement as Command in command mode.
  • VariousRunnerAs in the command patternInvoker,Various Statement will be issued to indicate the whole process of running JUnit test.
  • org.junit.internal.runners.statementThe subclass (specific command) of Statement is defined in the package to handle the method.
    Annotation, such as@Test,@Before,@After,@BeforeClass,@AfterClass;
// org.junit.runners.model.Statement
public abstract class Statement{

    public abstract void evalute() throws Throwable;

}

// BlockJUnit4ClassRunner In accordance with the command to deal with @Test, @Before, @After.../ / org.junit.runners.BlockJUnit4ClassRunnerPublic class BlockJUnit4ClassRunner extends ParentRunner< FrameworkMethoc> {... (slightly)Protected Statement MEthodBlock (FrameworkMethod method) {Object test;Try{Test = new ReflectIveCallable () {@OverrideProtected Object runReflectiveCall () throwsThrowable{Return createTest ();}}.run ();}catch (Throwable E) {Return new Fail (E);}/ / various StatementStateMent statement = methodInvoker (method, test);Statement = possiblyExpectingExceptions (methodTest, statement);Statement = withPotentialTimeout (method, test, statement);StateMent = withBefores (method, test, statement);Statement = withAfters (method, test, statement);Statement = withRules (method, test, statement);Return statement;}/ / basedReflection, execution of a non - parametric constructorProtected Object createTest () throws Exception{Return getTestClass ().GetOnlYConstructor ().NewInstance ();}/ / Statement buildersProtected Statement methodInvoker(FrameworkMethod method, Object test) {Return new InvokeMethod (method, test);}}

2.Statement” implementation class

  • org.junit.internal.runners.statementsUnder the package
    • ExpectException
    • Fail
    • FailOnTimeOut
    • InvokeMethod
    • RunAfters
    • RunBefores
// @Test(expected=IndexOutOfBoundsException.class)

// Source code org.junit.internal.runners.statements.ExpectExceptionPublic class ExpectException extends StatemEnt{Private final Statement next;Private final Class<? Extends Throwable> expected;Public ExpectException (Statement next, Class< extends Throwable> expected) {This.next = next;This.expected = expected;}@OverridePublic void evalute () throwS Exception{Boolean complete = false;Try{Next.evalute ();Complete = true;}catch (AssumptionViolatedException E) {Throw E;}catch (Throwable E) {If (! Expected.isAssignableFrom (e.getClass ())) {String messaGe = "Unexpected exception, expected<"+ expected.getName () + "> but w"As< "+ e.getClass ().GetName () + ">";Throw new ExceptiOn (messge, e);}}If (complete) {Throw new AssertionError ("EXpected exception: "+ expected.getName ());}}}

Reference material:

  • JUnit4.8.2 StatementCompound command
  • JUnit4.10 Statement

Leave a Reply

Your email address will not be published. Required fields are marked *