`
lvwenwen
  • 浏览: 932088 次
  • 性别: Icon_minigender_1
  • 来自: 魔都
社区版块
存档分类
最新评论

Junit源码分析设计模式(圣思园)

阅读更多

1.junit的设计思想

   1.从零开始来应用设计模式,然后一个接一个,直至你获得最终适合的系统架构

 

1.junit用到的设计模式

  1.模板方法模式

   1.父类角色:提供模板

   2.子类角色:为模板提供实现

   一.JUnit在TestCase类中应用了模板方法模式:

   public void runBare() throws Throwable {

setUp();

try {

runTest();

}

finally {

tearDown();

}

}

  2.适配器模式(Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作)

  1. 适配器模式(Adapter Pattern)主要分为三种:

  1) 基于类的继承方式

  2) 基于对象组合方式

  3) 缺省的适配器模式(AWT,Swing事件模型所采用的模式)(父类空实现,子类来实现)

 

   二. JUnit在TestCase类中应用了适配器(Adapter)模式:

     在runBare方法中,通过runTest方法将我们自己编写的testXXX方法进行了适配,

     public void runBare() throws Throwable {

setUp();

try {

runTest();

}

finally {

tearDown();

}

}

     使得JUnit可以执行我们自己编写的Test Case,runTest方法的实现如下:

/**

* Override to run the test and assert its state.

* @exception Throwable if any exception is thrown

*/

protected void runTest() throws Throwable {

assertNotNull(fName);

Method runMethod= null;

try {

// use getMethod to get all public inherited

// methods. getDeclaredMethods returns all

// methods of this class but excludes the

// inherited ones.

runMethod= getClass().getMethod(fName, null);

} catch (NoSuchMethodException e) {

fail("Method \""+fName+"\" not found");

}

if (!Modifier.isPublic(runMethod.getModifiers())) {

fail("Method \""+fName+"\" should be public");

}

 

try {

runMethod.invoke(this, new Class[0]);

}

catch (InvocationTargetException e) {

e.fillInStackTrace();

throw e.getTargetException();

}

catch (IllegalAccessException e) {

e.fillInStackTrace();

throw e;

}

}

  在runTest方法中,首先获得我们自己编写的testXXX方法所对应的Method对象(不带参数),

  然后检查该Method对象所对应的方法是否是public的,如果是则调用Method对象的invoke方法来去执行

  我们自己编写的testXXX方法。

 

  三。命令模式

    1. 将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化,对请求排队,

       或记录请求日志,以及支持可撤销的操作

 

  四.组合模式 (TestCase,TestSuit)

   1.组合模式又可以叫部分-整体模式

 

  五.如何看源代码

     1.调试加不断跟进,断点,debuge进去看源代码,2.看重点内容的源代码

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics