JUnit 4是一种流行的Java单元测试工具。使用JUnit 4和androidx.test包能够为Android程序编写单元测试和Instrument测试。下面我对JUnit 4在Android程序上的使用作一些简单的分析。android
首先简单介绍一下JUnit 4。JUnit 4是一个单元测试框架,简单来讲,JUnit 4的工做就是运行单元测试,而后将结果展现给用户。在这个过程当中涉及3个核心概念:表示单元测试的Statement、运行单元测试的Runner,以及接收测试数据,展现结果的RunNotifier。api
下面的代码摘录自org.junit.runners.ParentRunner,这段代码很好的展现了3个概念之间的关系:Runner运行Statement,将结果通知给RunNotifier。app
@Override public void run(final RunNotifier notifier) { EachTestNotifier testNotifier = new EachTestNotifier(notifier, getDescription()); try { Statement statement = classBlock(notifier); statement.evaluate(); } catch (AssumptionViolatedException e) { testNotifier.addFailedAssumption(e); } catch (StoppedByUserException e) { throw e; } catch (Throwable e) { testNotifier.addFailure(e); } }
org.junit.runner.Runner是一个抽象类,拥有两个抽象方法:框架
public abstract Description getDescription(); public abstract void run(RunNotifier notifier);
一般咱们不会直接接触到Runner类。若是想本身编写Runner,能够从org.junit.runners.BlockJUnit4ClassRunner派生。BlockJUnit4ClassRunner也是JUnit 4默认的Runner类。若是不想使用默认Runner,能够在测试类上添加注解@RunWith,设置测试须要的Runner。在测试Android程序时,一般会加上ide
@RunWith(AndroidJUnit4::class)
这行代码会使用androidx.test.ext.junit.runners.AndroidJUnit4做为Runner。AndroidJUnit4是一个包装类,它会检查系统属性java.runtime.name,若是其中包含字符串android,AndroidJUnit4会使用androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner做为实际的Runner。不然将使用org.robolectric.RobolectricTestRunner。而AndroidJUnit4ClassRunner正式派生自BlockJUnit4ClassRunner。函数
这里说一下@RunWith生效的过程。在运行测试的时候(好比Gradle使用的org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor任务),首先经过org.junit.runner.Request获得ClassRequest对象,ClassRequest内部经过AllDefaultPossibilitiesBuilder按照下列顺序工具
逐一寻找合适的RunnerBuilder,而后构建出Runner。听起来很复杂,实际上只需两行代码:单元测试
final Request request = Request.aClass((Class)testClass); final Runner runner = request.getRunner();
Statement表示一个测试用例,测试用例只有一个动做:执行。所以Statement类很是简单:测试
public abstract class Statement { public abstract void evaluate() throws Throwable; }
若是测试失败,Statement抛出异常。若是执行成功,no news is good news。Statement虽然简洁,经过组合能够构造出很是丰富的用法。好比下面这段代码
protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) { List<FrameworkMethod> befores = getTestClass().getAnnotatedMethods(Before.class); return befores.isEmpty() ? statement : new RunBefores(statement, befores, target); }
这段代码摘自BlockJUnit4ClassRunner类。熟悉JUnit 4的人一看就会明白,这是处理@Before注解的地方。实际上,根据这段代码,你们应该就能够猜出RunBefores类大体是什么样子了。为了例子的完整性,咱们把RunBefores类的代码也展现在这里。
package org.junit.internal.runners.statements; import java.util.List; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.Statement; public class RunBefores extends Statement { private final Statement next; private final Object target; private final List<FrameworkMethod> befores; public RunBefores(Statement next, List<FrameworkMethod> befores, Object target) { this.next = next; this.befores = befores; this.target = target; } @Override public void evaluate() throws Throwable { for (FrameworkMethod before : befores) { before.invokeExplosively(target); } next.evaluate(); } }
如同上面的@Before例子,JUnit 4的不少特性都是经过Statement组合实现的。下面这段代码也是BlockJUnit4ClassRunner的一部分,从这里咱们能够看出,@Rule也是经过封装Statement实现的。
protected Statement methodBlock(FrameworkMethod method) { Object test; try { test = new ReflectiveCallable() { @Override protected Object runReflectiveCall() throws Throwable { return createTest(); } }.run(); } catch (Throwable e) { return new Fail(e); } Statement statement = methodInvoker(method, test); statement = possiblyExpectingExceptions(method, test, statement); statement = withPotentialTimeout(method, test, statement); statement = withBefores(method, test, statement); statement = withAfters(method, test, statement); statement = withRules(method, test, statement); return statement; }
为了方便读者参考,咱们把org.junit.rules.TestRule接口和RunRules类贴在这里。
public interface TestRule { Statement apply(Statement base, Description description); }
package org.junit.rules; import org.junit.runner.Description; import org.junit.runners.model.Statement; public class RunRules extends Statement { private final Statement statement; public RunRules(Statement base, Iterable<TestRule> rules, Description description) { statement = applyAll(base, rules, description); } @Override public void evaluate() throws Throwable { statement.evaluate(); } private static Statement applyAll(Statement result, Iterable<TestRule> rules, Description description) { for (TestRule each : rules) { result = each.apply(result, description); } return result; } }
@BeforeClass注解也使用了Statement,下面的代码来自ParentRunner。ParentRunner是BlockJUnit4ClassRunner的父类。
protected Statement classBlock(final RunNotifier notifier) { Statement statement = childrenInvoker(notifier); if (!areAllChildrenIgnored()) { statement = withBeforeClasses(statement); statement = withAfterClasses(statement); statement = withClassRules(statement); } return statement; }
Runner负责执行测试用例,测试的结果通知给RunNotifier。RunNotifier是一个RunListener集合,测试信息最终由RunListener处理。咱们运行单元测试时,控制台输出的信息就是由TextListener生成的。
public class RunListener { public void testRunStarted(Description description) throws Exception {} public void testRunFinished(Result result) throws Exception {} public void testStarted(Description description) throws Exception {} public void testFinished(Description description) throws Exception {} public void testFailure(Failure failure) throws Exception {} public void testAssumptionFailure(Failure failure) {} public void testIgnored(Description description) throws Exception {} }
在运行测试时,ParentRunner负责将测试事件通知给RunNotifier。
protected final void runLeaf(Statement statement, Description description, RunNotifier notifier) { EachTestNotifier eachNotifier = new EachTestNotifier(notifier, description); eachNotifier.fireTestStarted(); try { statement.evaluate(); } catch (AssumptionViolatedException e) { eachNotifier.addFailedAssumption(e); } catch (Throwable e) { eachNotifier.addFailure(e); } finally { eachNotifier.fireTestFinished(); } }
在前面介绍Runner、Statement和RunNotifier时,已经不止一次提到了BlockJUnit4ClassRunner和ParentRunner。这是两个很是重要的类,有了前面的基础,在这里咱们能够作一些深刻分析。
ParentRunner是一个抽象类,它有两个重要的函数:run和runLeaf,这两个函数在前面已经介绍过了。ParentRunner还有两个重要接口:
protected abstract void runChild(T child, RunNotifier notifier); protected abstract List<T> getChildren();
runChild负责执行一个测试方法。正如方法classBlock所暗示的,ParentRunner运行一个测试类。ParentRunner将一组测试方法看做本身的child。child经过getChildren得到。ParentRunner将各个child所表明的测试用例经过childrenInvoker封装成一个Statement,在加上@BeforeClass和@AfterClass,构形成最终的Statement。
@Override public void run(final RunNotifier notifier) { EachTestNotifier testNotifier = new EachTestNotifier(notifier, getDescription()); try { Statement statement = classBlock(notifier); statement.evaluate(); } catch (AssumptionViolatedException e) { testNotifier.addFailedAssumption(e); } catch (StoppedByUserException e) { throw e; } catch (Throwable e) { testNotifier.addFailure(e); } } protected Statement classBlock(final RunNotifier notifier) { Statement statement = childrenInvoker(notifier); if (!areAllChildrenIgnored()) { statement = withBeforeClasses(statement); statement = withAfterClasses(statement); statement = withClassRules(statement); } return statement; } protected Statement childrenInvoker(final RunNotifier notifier) { return new Statement() { @Override public void evaluate() { runChildren(notifier); } }; } private void runChildren(final RunNotifier notifier) { final RunnerScheduler currentScheduler = scheduler; try { for (final T each : getFilteredChildren()) { currentScheduler.schedule(new Runnable() { public void run() { ParentRunner.this.runChild(each, notifier); } }); } } finally { currentScheduler.finished(); } }
BlockJUnit4ClassRunner派生自ParentRunner<FrameworkMethod>。一样如方法methodBlock所暗示,BlockJUnit4ClassRunner更关注测试方法层面的工做:根据注解寻找测试方法,并将测试方法封装成Statement。
@Override protected void runChild(final FrameworkMethod method, RunNotifier notifier) { Description description = describeChild(method); if (isIgnored(method)) { notifier.fireTestIgnored(description); } else { runLeaf(methodBlock(method), description, notifier); } } protected Statement methodBlock(FrameworkMethod method) { Object test; try { test = new ReflectiveCallable() { @Override protected Object runReflectiveCall() throws Throwable { return createTest(); } }.run(); } catch (Throwable e) { return new Fail(e); } Statement statement = methodInvoker(method, test); statement = possiblyExpectingExceptions(method, test, statement); statement = withPotentialTimeout(method, test, statement); statement = withBefores(method, test, statement); statement = withAfters(method, test, statement); statement = withRules(method, test, statement); return statement; }
这里简单说明一下methodBlock方法。FrameworkMethod表明一个Java方法,也就是@Test测试方法。methodBlock首先创建测试类实例,而后用methodInvoker将测试方法和类实例封装成Statement,再加上@Before等注解,构造出完成的Statement。
如今介绍一下Android单元测试中遇到的AndroidJUnit4ClassRunner。AndroidJUnit4ClassRunner派生自BlockJUnit4ClassRunner,核心代码是重写了方法methodInvoker。
@Override protected Statement methodInvoker(FrameworkMethod method, Object test) { if (UiThreadStatement.shouldRunOnUiThread(method)) { return new UiThreadStatement(super.methodInvoker(method, test), true); } return super.methodInvoker(method, test); }
咱们知道methodInvoker将测试方法封装为Statement,AndroidJUnit4ClassRunner封装的UIThreadStatement是作什么用的呢?顾名思义,在UI线程中测试。
@Override public void evaluate() throws Throwable { if (runOnUiThread) { final AtomicReference<Throwable> exceptionRef = new AtomicReference<>(); runOnUiThread( new Runnable() { @Override public void run() { try { base.evaluate(); } catch (Throwable throwable) { exceptionRef.set(throwable); } } }); Throwable throwable = exceptionRef.get(); if (throwable != null) { throw throwable; } } else { base.evaluate(); } }
shouldRunOnUiThread的判断标准也很简单,检查是否有UiThread注解。
public static boolean shouldRunOnUiThread(FrameworkMethod method) { Class<? extends Annotation> deprecatedUiThreadTestClass = loadUiThreadClass("android.test.UiThreadTest"); if (hasAnnotation(method, deprecatedUiThreadTestClass)) { return true; } else { // to avoid circular dependency on Rules module use the class name directly @SuppressWarnings("unchecked") // reflection Class<? extends Annotation> uiThreadTestClass = loadUiThreadClass("androidx.test.annotation.UiThreadTest"); if (hasAnnotation(method, deprecatedUiThreadTestClass) || hasAnnotation(method, uiThreadTestClass)) { return true; } } return false; }
对于Instrument测试,还须要在测试类中声明ActivityTestRule。
@get:Rule val activityRule = ActivityTestRule(MainActivity::class.java)
ActivityTestRule将测试方法封装为AndroidStatement。AndroidStatement在运行前发送指令到设备,启动应用。在测试后关闭应用。
@Override public void evaluate() throws Throwable { MonitoringInstrumentation instrumentation = ActivityTestRule.this.instrumentation instanceof MonitoringInstrumentation ? (MonitoringInstrumentation) ActivityTestRule.this.instrumentation : null; try { if (activityFactory != null && instrumentation != null) { instrumentation.interceptActivityUsing(activityFactory); } if (launchActivity) { launchActivity(getActivityIntent()); } base.evaluate(); } finally { if (instrumentation != null) { instrumentation.useDefaultInterceptingActivityFactory(); } T hardActivityRef = activity.get(); if (hardActivityRef != null) { finishActivity(); } activityResult = null; ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(lifecycleCallback); } }
除了AndroidTestRule,androidx还提供了下列规则:
规则 | 说明 |
---|---|
GrantPermissionRule | 运行测试方法前申请权限。 |
ProviderTestRule | 测试ContentProvider。 |
ServiceTestRule | 测试服务。 |
PortForwardRule | 转发端口。 |
分析这些规则,只要看apply方法返回了什么Statement,以及这些Statement的evaluate作了什么。