public class

RunBefores

extends UiThreadStatement

 java.lang.Object

↳Statement

androidx.test.internal.runner.junit4.statement.UiThreadStatement

↳androidx.test.internal.runner.junit4.statement.RunBefores

Gradle dependencies

compile group: 'androidx.test', name: 'runner', version: '1.5.0-alpha03'

  • groupId: androidx.test
  • artifactId: runner
  • version: 1.5.0-alpha03

Artifact androidx.test:runner:1.5.0-alpha03 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test:runner com.android.support.test:runner

Overview

@UiThreadTest aware implementation of RunBefores.

Summary

Constructors
publicRunBefores(FrameworkMethod method, Statement next, java.util.List<FrameworkMethod> befores, java.lang.Object target)

Run all non-overridden @Before methods on this class and superclasses before running next; if any throws an Exception, stop execution and pass the exception on.

Methods
public voidevaluate()

from UiThreadStatementisRunOnUiThread, runOnUiThread, shouldRunOnUiThread
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public RunBefores(FrameworkMethod method, Statement next, java.util.List<FrameworkMethod> befores, java.lang.Object target)

Run all non-overridden @Before methods on this class and superclasses before running next; if any throws an Exception, stop execution and pass the exception on.

@Before methods that also annotated with @UiThreadTest will be executed on the UI Thread.

Parameters:

next: the original statement
befores: methods annotated with @Before
target: the test case instance

Methods

public void evaluate()

Source

/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 */

package androidx.test.internal.runner.junit4.statement;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

/** <code>@UiThreadTest</code> aware implementation of {@link RunBefores}. */
public class RunBefores extends UiThreadStatement {
  private final Statement next;

  private final Object target;

  private final List<FrameworkMethod> befores;

  /**
   * Run all non-overridden {@code @Before} methods on this class and superclasses before running
   * {@code next}; if any throws an Exception, stop execution and pass the exception on.
   *
   * <p>{@code @Before} methods that also annotated with <code>@UiThreadTest</code> will be executed
   * on the UI Thread.
   *
   * @param next the original statement
   * @param befores methods annotated with {@code @Before}
   * @param target the test case instance
   */
  public RunBefores(
      FrameworkMethod method, Statement next, List<FrameworkMethod> befores, Object target) {
    super(next, shouldRunOnUiThread(method));
    this.next = next;
    this.befores = befores;
    this.target = target;
  }

  @Override
  public void evaluate() throws Throwable {
    final AtomicReference<Throwable> exceptionRef = new AtomicReference<>();
    for (final FrameworkMethod before : befores) {
      if (shouldRunOnUiThread(before)) {
        runOnUiThread(
            new Runnable() {
              @Override
              public void run() {
                try {
                  before.invokeExplosively(target);
                } catch (Throwable throwable) {
                  exceptionRef.set(throwable);
                }
              }
            });

        // if any Exception thrown, stop execution and pass the exception on.
        Throwable throwable = exceptionRef.get();
        if (throwable != null) {
          throw throwable;
        }
      } else {
        before.invokeExplosively(target);
      }
    }

    next.evaluate();
  }
}