`
echozhjun
  • 浏览: 47936 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

源码阅读小记(JUNIT)

阅读更多
<!--<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 != nullthrow 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中调用,就可以在这里捕获这些失败和错误的异常信息,管理好失败和错误列表,同时通知观察者了。 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics