surefire 拉起 junit 单元测试类 源码阅读(二)
根据上一篇blog,看到日志1流程:
surefire.ForkedBooter -> ForkedBooter.runSuitesInProcess
-> ForkedBooter.invokeProviderInSameClassLoader
-> surefire.junit4.JUnit4Provider.invoke -> executeTestSet -> executeWithRerun
-> execute
-> junit.runners.ParentRunner.run -> evaluate -> runChildren -> schedule -> run
-> BlockJUnit4ClassRunner.runChild
-> junit.runners.ParentRunner.runLeaf
-> junit.rules.RunRules.evaluate
-> org.junit.internal.runners.statements.RunBefores.evaluate
-> junit.runners.model.FrameworkMethod.invokeExplosively
-> junit.internal.runners.model.ReflectiveCallable.run
-> junit.runners.model.FrameworkMethod.runReflectiveCall
这是 一个junit的版本调用过程,不同junit 版本,调用类不同,如日志2
-> ForkedBooter.invokeProviderInSameClassLoader
-> surefire.junit4.JUnit4Provider.invoke -> executeTestSet -> executeWithRerun
-> execute
-> junit.runners.ParentRunner.run -> evaluate -> runChildren -> schedule -> run
-> BlockJUnit4ClassRunner.runChild
-> org.junit.internal.runners.statements.InvokeMethod.evaluate
-> org.junit.runners.model.FrameworkMethod.invokeExplosively
-> org.junit.internal.runners.model.ReflectiveCallable.run
-> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall
InvokeMethod 了类代码:
public class InvokeMethod extends Statement {private final FrameworkMethod fTestMethod;private Object fTarget;public InvokeMethod(FrameworkMethod testMethod, Object target) {fTestMethod= testMethod;fTarget= target;}@Overridepublic void evaluate() throws Throwable {fTestMethod.invokeExplosively(fTarget);}
Statement:
public abstract class Statement {/*** Run the action, throwing a {@code Throwable} if anything goes wrong.*/public abstract void evaluate() throws Throwable;
}
FrameworkMethod代码:
public class FrameworkMethod {private final Method fMethod;/*** Returns a new {@code FrameworkMethod} for {@code method}*/public FrameworkMethod(Method method) {fMethod= method;}/*** Returns the underlying Java method*/public Method getMethod() {return fMethod;}/*** Returns the result of invoking this method on {@code target} with* parameters {@code params}. {@link InvocationTargetException}s thrown are* unwrapped, and their causes rethrown.*/public Object invokeExplosively(final Object target, final Object... params)throws Throwable {return new ReflectiveCallable() {@Overrideprotected Object runReflectiveCall() throws Throwable {return fMethod.invoke(target, params);}}.run();}/*** Returns the method's name*/public String getName() {return fMethod.getName();}/*** Adds to {@code errors} if this method:* <ul>* <li>is not public, or* <li>takes parameters, or* <li>returns something other than void, or* <li>is static (given {@code isStatic is false}), or* <li>is not static (given {@code isStatic is true}).*/public void validatePublicVoidNoArg(boolean isStatic, List<Throwable> errors) {validatePublicVoid(isStatic, errors);if (fMethod.getParameterTypes().length != 0)errors.add(new Exception("Method " + fMethod.getName() + " should have no parameters"));}/*** Adds to {@code errors} if this method:* <ul>* <li>is not public, or* <li>returns something other than void, or* <li>is static (given {@code isStatic is false}), or* <li>is not static (given {@code isStatic is true}).*/public void validatePublicVoid(boolean isStatic, List<Throwable> errors) {if (Modifier.isStatic(fMethod.getModifiers()) != isStatic) {String state= isStatic ? "should" : "should not";errors.add(new Exception("Method " + fMethod.getName() + "() " + state + " be static"));}if (!Modifier.isPublic(fMethod.getDeclaringClass().getModifiers()))errors.add(new Exception("Class " + fMethod.getDeclaringClass().getName() + " should be public"));if (!Modifier.isPublic(fMethod.getModifiers()))errors.add(new Exception("Method " + fMethod.getName() + "() should be public"));if (fMethod.getReturnType() != Void.TYPE)errors.add(new Exception("Method " + fMethod.getName() + "() should be void"));}boolean isShadowedBy(List<FrameworkMethod> results) {for (FrameworkMethod each : results)if (isShadowedBy(each))return true;return false;}private boolean isShadowedBy(FrameworkMethod each) {if (!each.getName().equals(getName()))return false;if (each.getParameterTypes().length != getParameterTypes().length)return false;for (int i= 0; i < each.getParameterTypes().length; i++)if (!each.getParameterTypes()[i].equals(getParameterTypes()[i]))return false;return true;}@Overridepublic boolean equals(Object obj) {if (!FrameworkMethod.class.isInstance(obj))return false;return ((FrameworkMethod) obj).fMethod.equals(fMethod);}@Overridepublic int hashCode() {return fMethod.hashCode();}/*** Returns true iff this is a no-arg method that returns a value assignable* to {@code type}*/public boolean producesType(Class<?> type) {return getParameterTypes().length == 0&& type.isAssignableFrom(fMethod.getReturnType());}private Class<?>[] getParameterTypes() {return fMethod.getParameterTypes();}/*** Returns the annotations on this method*/public Annotation[] getAnnotations() {return fMethod.getAnnotations();}/*** Returns the annotation of type {@code annotationType} on this method, if* one exists.*/public <T extends Annotation> T getAnnotation(Class<T> annotationType) {return fMethod.getAnnotation(annotationType);}
}
ReflectiveCallable类:
public abstract class ReflectiveCallable {public Object run() throws Throwable {try {return runReflectiveCall();} catch (InvocationTargetException e) {throw e.getTargetException();}}protected abstract Object runReflectiveCall() throws Throwable;
}
surefire 拉起 junit 单元测试类 源码阅读(二)相关推荐
- surefire 拉起 junit 单元测试类 源码阅读(一)
根据surefire 拉起Junit单元测试类 输出的报错日志 跟踪执行过程: 日志1: java.lang.reflect.InvocationTargetExceptionat sun.refle ...
- mybatis源码阅读(二):mybatis初始化上
转载自 mybatis源码阅读(二):mybatis初始化上 1.初始化入口 //Mybatis 通过SqlSessionFactory获取SqlSession, 然后才能通过SqlSession与 ...
- Java String类源码阅读笔记
文章目录 一.前置 二.String类源码解析 1.String类继承关系 2.成员变量 3.构造方法 4.长度/判空 5.取字符 6.比较 7.包含 8.hashCode 9.查询索引 10.获取子 ...
- LeGo-LOAM激光雷达定位算法源码阅读(二)
文章目录 1.featureAssociation框架 1.1节点代码主体 1.2 FeatureAssociation构造函数 1.3 runFeatureAssociation()主体函数 2.重 ...
- surefire 拉起testng单元测试类的源码流程阅读(二)
这里是基于surefire 2.19.1版本分析的. 还是根据surefire 拉起单元测试执行报错的日志展示的执行过程分析 java.lang.instrument.IllegalClassForm ...
- surefire 拉起testng单元测试类的源码流程阅读(一)
这里分析是基于2.5surefire 版本. 首先拿surefire 拉起单元测试报错日志 分析: Caused by: java.io.IOException: Error while instru ...
- 如何模拟一个XMLHttpRequest请求用于单元测试——nise源码阅读与分析
概述 在我们进行单元测试的过程中,如果我们需要对一些HTTP接口进行相关的业务测试,那么我们就需要来模拟HTTP请求的发送与响应,否则我们就无法完成测试的闭环. 目前,有许许多多的测试框架都提供了模拟 ...
- java工具类源码阅读,java学习日记第二天(实用的工具类和源码解析一Arrays)
本帖最后由 三木猿 于 2020-9-18 11:17 编辑 每日名言 学者须先立志.今日所以悠悠者,只是把学问不曾做一件事看,遇事则且胡乱恁地打过了,此只是志不立. --朱熹 工作中经常会用到一些工 ...
- DBFace: 源码阅读(二)
上篇链接 看LZ上篇博客的时间竟然是7月18日,着实是懈怠了,其实有很多东西需要总结归纳,这周末就补一下之前欠的债吧 上篇主要介绍了DBFace的大体框架,这篇主要介绍数据的预处理部分 5. 数据预处 ...
最新文章
- oracle 11g完全安装教程(CentOS)
- 关于js中的时间处理
- python小白-day4递归和算法基础
- junit与spring-data-redis 版本对应成功的
- SpringMVC+Json构建基于Restful风格的应用
- grpc 客户端的context 服务端获取不到_MLamp;DEV[10] | gRPC的应用
- golang 绘图库_golang入门-- 一个2D的图形库学习
- 企业之HA~cluster
- php new static,PHP中new static()与new self()的区别异同分析
- node.js中实现同步操作的3种实现方法
- 【前端】js代码模拟用户键盘鼠标输入
- LinkedIn领英上的几度人脉是什么意思?如何突破领英人脉限制高效率开发客户?
- 如何编写系统设计说明书
- [Luogu1970] 花匠 [贪心/dp]
- 35岁的网络工程师入行需要注意什么?
- 简洁易懂的配置Go开发环境MacOS
- [NSSRound#8 Basic]MyDoor
- pygame-KidsCanCode系列jumpy-part9-使用spritesheet
- 操作系统C语言模拟内存分配算法的模拟实现
- 埃安崛起,新能源汽车下半场
热门文章
- tornado.httpclient.HTTPClient()的用法
- flask和ajax通信详细步骤与完整代码
- 不要再次进行阅读的计算机论文与理由(持续更新中)
- python调用mysql中的自定义function并且返回结果
- 5.4 加权最小二乘法
- 深度学习(三十七)——CenterNet, Anchor-Free, NN Quantization
- 机器学习(八)——在线学习、K-Means算法、混合高斯模型和EM算法
- html iframe视频自动播放的属性,iframe通用js播放器myplayer.js加自动播放参数
- MZOJ 1134: 二叉苹果树
- Could not load java.net.BindException错误解决