您可以在这里快速查找:


 
您的位置: 编程学习 > java教程 > 200509
文章分类

Java技术
2005: 03 04 05 06 07 08
09 10 11 12
2006: 01 02

Asp.net
2005: 07 08 09 10 11 12
2006: 01 02

VB编程
2006: 02

Asp编程
2005: 11 12
2006: 01 02

C++/VC
2005: 10 11 12
2006: 01 02

Delphi
2005: 12
2006: 01 02

其它

 本文章适合所有读者

引入Mock类的简单方法

zhangweis
public class A {
  public void action() {
    ....
    //findSomeThingThroghClassB logic;
    ....
  }
}
先把A中需要B的部分findSomeThingThroghClassB logic提取成一个protected方法
public class A {
  public void action() {
    ....
    findSomeThingThroghClassB();
    ....
  }
    protected void findSomeThingThroghClassB() {
    //findSomeThingThroghClassB logic;
}
}
然后在TestCase中创建A时
A a = new A() {
    protected void findSomeThingThroghClassB() {
        mockIt();
    }
}
这样在测试中就可以直接使用mock了。
相比其它方法,这种方法对A类的改动较小,而且不需要新增类变量。