<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 /**
2 * Creates a default TestResult object
3 *
4 * @see TestResult
5 */
6 protected TestResult createResult() {
7 return new TestResult();
8 }
9 /**
10 * A convenience method to run this test, collecting the results with a
11 * default TestResult object.
12 *
13 * @see TestResult
14 */
15 public TestResult run() {
16 TestResult result= createResult();
17 run(result);
18 return result;
19 }
20 /**
21 * Runs the test case and collects the results in TestResult.
22 */
23 public void run(TestResult result) {
24 result.run(this);
25 }
这三个函数,如果要是我自己写的话,估计就写成一个函数了。对功能的拆分要细呀,这样才能达到重用的目的。而通常一开始程序都会写成一个程序,然后再根据功能的细分重构成三个不同的函数。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 /**
2 * Runs the test case and collects the results in TestResult.
3 */
4 public void run(TestResult result) {
5 result.run(this);
6 }
7 /**
8 * Runs the bare test sequence.
9 * @throws Throwable if any exception is thrown
10 */
11 public void runBare() throws Throwable {
12 Throwable exception= null;
13 setUp();
14 try {
15 runTest();
16 } catch (Throwable running) {
17 exception= running;
18 }
19 finally {
20 try {
21 tearDown();
22 } catch (Throwable tearingDown) {
23 if (exception == null) exception= tearingDown;
24 }
25 }
26 if (exception != null) throw exception;
27 }
28 /**
29 * Override to run the test and assert its state.
30 * @throws Throwable if any exception is thrown
31 */
32 protected void runTest() throws Throwable {
33 assertNotNull("TestCase.fName cannot be null", fName); // Some VMs crash when calling getMethod(null,null);
34 Method runMethod= null;
35 try {
36 // use getMethod to get all public inherited
37 // methods. getDeclaredMethods returns all
38 // methods of this class but excludes the
39 // inherited ones.
40 runMethod= getClass().getMethod(fName, (Class[])null);
41 } catch (NoSuchMethodException e) {
42 fail("Method \""+fName+"\" not found");
43 }
44 if (!Modifier.isPublic(runMethod.getModifiers())) {
45 fail("Method \""+fName+"\" should be public");
46 }
47
48 try {
49 runMethod.invoke(this);
50 }
51 catch (InvocationTargetException e) {
52 e.fillInStackTrace();
53 throw e.getTargetException();
54 }
55 catch (IllegalAccessException e) {
56 e.fillInStackTrace();
57 throw e;
58 }
59 }
在TestCase中,run函数的参数为TestResult,并且将自己传给TestResult.run方法作为参数。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 /**
2 * Runs a TestCase.
3 */
4 protected void run(final TestCase test) {
5 startTest(test);
6 Protectable p= new Protectable() {
7 public void protect() throws Throwable {
8 test.runBare();
9 }
10 };
11 runProtected(test, p);
12
13 endTest(test);
14 }
15 /**
16 * Runs a TestCase.
17 */
18 public void runProtected(final Test test, Protectable p) {
19 try {
20 p.protect();
21 }
22 catch (AssertionFailedError e) {
23 addFailure(test, e);
24 }
25 catch (ThreadDeath e) { // don't catch ThreadDeath by accident
26 throw e;
27 }
28 catch (Throwable e) {
29 addError(test, e);
30 }
31 }
然后在TestResult中,再调用TestCase.runBare方法,执行真正的测试用例。
之所以这样来来回回的原因,目前我想到的:TestResult类主要的作用是维护测试失败和测试错误的情况,当然失败和错误的曾删改也在这个类中了。增删改同时就需要通知观察者执行相应的操作。正是因为这样,所以定义了Protectable这个接口来管理失败和错误的情况。在TestResult中调用,就可以在这里捕获这些失败和错误的异常信息,管理好失败和错误列表,同时通知观察者了。
分享到:
相关推荐
junit 的源码jar包 junit 的源码jar包 junit 的源码jar包
基于java的开发源码-junit4 jar包 Java单元测试框架绿色免费版.zip 基于java的开发源码-junit4 jar包 Java单元测试框架绿色免费版.zip 基于java的开发源码-junit4 jar包 Java单元测试框架绿色免费版.zip 基于java的...
此文件包含源代码(简单加减乘除),测试源码(junit4框架下的单元测试源码),入门级学习材料。Eclipse开发环境下
junit in action 源码 java版
JUnit有它自己的JUnit扩展生态圈。多数Java的开发环境都已经集成了JUnit作为单元测试的工具。 [1] JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework)。Junit测试是...
JUnit4.8.1源码包 版本4.8.1 只有源码。
这个是junit4 的完整源代码,其中解压以后的文件中找到javadoc那个是文档。
junit4测试源码,看浪曦JUNIT4笔记
junit-4.11.jar junit-4.11-sources.jar 单元测试jar包,含源码
Junit Recipes书在CDSN上能找到,所以这里只提供源码。
Junit jar包以及 Junit 源码包
junit4.12源码,便于查看源码,重写方法,为己所用。
重点讲到了junit里用到了那些设计模式.
SpringBoot中整合Junit测试示例源码
JUnit测试源码JUnit测试源码JUnit测试源码
JUnit4.7包及源码 JUnit4.8包 JUnit4.11包及源码,以及JUnit文档
junit-4.4包+源码
junit in action 第2版源码,使用maven构件工程
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage,包含依赖包:junit-jupiter-5.5.1.jar,junit-jupiter-engine-5.5.1.jar,junit-jupiter-params-5.5.1.jar,junit-platform-launcher-1.5.1.jar,junit-...
Spring是如何整合JUnit的?JUnit源码关联延伸阅读.doc