public final class

AndroidJUnit4

extends Runner

 java.lang.Object

↳Runner

↳androidx.test.runner.AndroidJUnit4

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

Androidx class mapping:

androidx.test.runner.AndroidJUnit4 android.support.test.runner.AndroidJUnit4

Overview

Aliases the current default Android JUnit 4 class runner, for future-proofing. If future versions of JUnit change the default Runner class, they will also change the definition of this class. Developers wanting to explicitly tag a class as an Android JUnit 4 class should use @RunWith(AndroidJUnit4.class)

Summary

Constructors
publicAndroidJUnit4(java.lang.Class<java.lang.Object> klass)

Used when executed with standard junit runner.

publicAndroidJUnit4(java.lang.Class<java.lang.Object> klass, AndroidRunnerParams runnerParams)

Constructs a new instance of the default runner

Methods
public voidfilter(Filter filter)

public DescriptiongetDescription()

public voidrun(RunNotifier runNotifier)

public voidsort(Sorter sorter)

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

Constructors

public AndroidJUnit4(java.lang.Class<java.lang.Object> klass, AndroidRunnerParams runnerParams)

Constructs a new instance of the default runner

public AndroidJUnit4(java.lang.Class<java.lang.Object> klass)

Used when executed with standard junit runner. Will attempt to delegate to RobolectricTestRunner or delegate provided by android.junit.runner system property.

Methods

public Description getDescription()

public void run(RunNotifier runNotifier)

public void filter(Filter filter)

public void sort(Sorter sorter)

Source

/*
 * Copyright (C) 2014 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.runner;

import android.util.Log;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
import androidx.test.internal.util.AndroidRunnerParams;
import java.lang.reflect.InvocationTargetException;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.Filterable;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.manipulation.Sortable;
import org.junit.runner.manipulation.Sorter;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.InitializationError;

/**
 * Aliases the current default Android JUnit 4 class runner, for future-proofing. If future versions
 * of JUnit change the default Runner class, they will also change the definition of this class.
 * Developers wanting to explicitly tag a class as an Android JUnit 4 class should use
 * {@code @RunWith(AndroidJUnit4.class)}
 *
 * @deprecated use androidx.test.ext.junit.runners.AndroidJUnit4 instead.
 */
@Deprecated
public final class AndroidJUnit4 extends Runner implements Filterable, Sortable {

  private static final String TAG = "AndroidJUnit4";

  private final Runner delegate;

  /**
   * Constructs a new instance of the default runner
   *
   * @hide
   */
  @RestrictTo(Scope.LIBRARY)
  public AndroidJUnit4(Class<?> klass, AndroidRunnerParams runnerParams)
      throws InitializationError {
    // this is expected to be called when in Android environment.
    delegate = new AndroidJUnit4ClassRunner(klass, runnerParams);
  }

  /**
   * Used when executed with standard junit runner. Will attempt to delegate to
   * RobolectricTestRunner or delegate provided by android.junit.runner system property.
   */
  public AndroidJUnit4(Class<?> klass) throws InitializationError {
    delegate = loadRunner(klass);
  }

  private static Runner loadRunner(Class<?> testClass) throws InitializationError {
    String runnerClassName =
        System.getProperty("android.junit.runner", "org.robolectric.RobolectricTestRunner");
    return loadRunner(testClass, runnerClassName);
  }

  private static Runner loadRunner(Class<?> testClass, String className)
      throws InitializationError {
    try {
      @SuppressWarnings("unchecked")
      Class<? extends Runner> runnerClass = (Class<? extends Runner>) Class.forName(className);
      return runnerClass.getConstructor(Class.class).newInstance(testClass);
    } catch (ClassNotFoundException e) {
      Log.e(TAG, className + " could not be loaded", e);
      // fall through
    } catch (NoSuchMethodException e) {
      Log.e(TAG, className + " could not be loaded", e);
      // fall through
    } catch (IllegalAccessException e) {
      Log.e(TAG, className + " could not be loaded", e);
      // fall through
    } catch (InstantiationException e) {
      Log.e(TAG, className + " could not be loaded", e);
      // fall through
    } catch (InvocationTargetException e) {
      Log.e(TAG, className + " could not be loaded", e);
      // fall through
    }
    throw new InitializationError(
        String.format(
            "Attempted to use AndroidJUnit4 with standard JUnit runner and delegate runner '%s'"
            + "could not be loaded. Check your build configuration.", className));
  }

  @Override
  public Description getDescription() {
    return delegate.getDescription();
  }

  @Override
  public void run(RunNotifier runNotifier) {
    delegate.run(runNotifier);
  }

  @Override
  public void filter(Filter filter) throws NoTestsRemainException {
    ((Filterable) delegate).filter(filter);
  }

  @Override
  public void sort(Sorter sorter) {
    ((Sortable) delegate).sort(sorter);
  }
}