junit 4只是简单的加了一个@Test,程序是怎么跑起来的
在写测试用例的时候,突然很疑惑,为什么我只是加了一个@Test的注解,就能运行一个程序。我们知道,main方法才是一个java程序的起点。那junit4的测试用例是怎么跑起来的呢。
为了解决这个疑惑,我就自己写测试用例debug调试了下。
测试用例如下:
package com.onlyou.olyfinance.remote.base; import org.junit.Test; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; /** * 模拟dubbo消费端远程调用进行测试 * Created by cd_huang on 2016/12/26. */ @ContextConfiguration(locations = { "classpath:test/spring/appCtx-dubbo.xml" }) public class BaseServiceTestRemote extends AbstractJUnit4SpringContextTests { @Test public void testRun() { System.out.println("run~~~~~~"); } }
用intellij运行后,在System.out.println("run~~~~~~")处设置断点,然后我们直接看方法调用:
"main@1" prio=5 tid=0x1 nid=NA runnable java.lang.Thread.State: RUNNABLE at com.onlyou.olyfinance.remote.base.BaseServiceTestRemote.testRun(BaseServiceTestRemote.java:16) at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java:-1) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) at org.junit.runner.JUnitCore.run(JUnitCore.java:160) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
程序的起点:at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)。
说明是IDE的集成了junit插件,这也是为什么我们没有些main方法为什么能运行程序的原因。
看一下JUnitCore的run()方法:
public Result run(Runner runner) { Result result = new Result(); RunListener listener = result.createListener(); fNotifier.addFirstListener(listener); try { fNotifier.fireTestRunStarted(runner.getDescription()); runner.run(fNotifier); fNotifier.fireTestRunFinished(result); } finally { removeListener(listener); } return result; }debug后查看runner的实际类型是SpringJUnit4ClassRunner:
那么,SpringJUnit4ClassRunner这个类是怎么来的。
我们的测试用例的父类是抽象类AbstractJUnit4SpringContextTests,源码如下:
@RunWith(SpringJUnit4ClassRunner.class) @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class }) public abstract class AbstractJUnit4SpringContextTests implements ApplicationContextAware { ... }
也就是,ide的junit插件在运行时会实例化@RunWith注解对应的类,把测试用例的类和方法的信息附带上。
然后中间有很多的代码,比如去获得@Rule,@Before,@After等Statement配置去实用junit的强大功能。
最终就是通过反射调用我们的测试用例。
由于本人水平有限,没有对junit4做更较真的研究,有兴趣了解更多的人可以看这篇博客:http://www.jianshu.com/p/ad524e211ef3
上一篇: Go基础系列:简单数据类型
下一篇: 品友互动受邀2018商汤人工智能峰会