如何编写模拟对象?

Jul*_*iet 3 unit-testing mocking

我的第一个编程工作向我介绍了单元测试和模拟对象的概念,但总有一些问题.

假设我们正在编写一个银行应用程序,并且需要模拟BankAccount对象:


    // boilerplate code
    public interface IBankAccount {
        void Deposit(int amount);
        void Withdrawal(int amount);
        int getBalance();
        int getAccountNumber();
    }

    public interface IBankAccountFactory {
        IBankAccount getAccount(int accountNumber);
    }

    public class ProductionBankAccountFactory implements IBankAccountFactory { 
        public IBankAccount getAccount(int accountNumber) {
            return new RealBankAccount(accountNumber);
        }
    }

    public class MockBankAccountFactory implements IBankAccountFactory {
        public IBankAccount getAccount(int accountNumber) {
            return new MockBankAccount(accountNumber);
        }
    }

    public static class BankAccountFactory {
        // ewww, singletons!
        public static IBankAccountFactory Instance;
    }

    // finally, my actual business objects
    public class MockBankAccount implements IBankAccount {
        public MockBankAccount(int accountNumber) { ... }
        // interface implementation
    }

    public class RealBankAccount implements IBankAccount {
        public RealBankAccount(int accountNumber) { ... }
        // interface implementation
    }
Run Code Online (Sandbox Code Playgroud)

每个班级都有一个目的:

  • 存在工厂和工厂接口以将构造函数包装到我们的模拟和真实对象中.
  • 静态BankAccountFactory类允许我们BankAccountFactory.Instance分别在生产应用程序或测试开始时分配IRealBankAccountFactory或MockBankAccountFactory的实例.
  • 一切设置正确后,任何类都可以通过调用以下方式获取IBankAccount实例:

    BankAccountFactory.Instance.getAccount(accountNum);
Run Code Online (Sandbox Code Playgroud)

这有效,但它会产生大量的样板代码.我不应该为每个我想要模拟的类编写5个新类.我相信有一种更简单的方法,所以我不得不问SO社区:

是否有更好或更优先的方式来编写模拟对象?

[编辑添加:]我很欣赏模拟和DI框架的链接,但是现在我正在研究500 KLOC应用程序,并且至少有60%的代码包含上述样式的样板模拟类.

我只想减少代码库的大小而不重写Yet-Another-Framework™的大块代码,所以它有助于我更多地看到手工编写的模拟类.:)

cas*_*One 8

更好的方法是让别人写它.这里的一些选择是:

Moq - http://code.google.com/p/moq/

Rhino Mocks - http://ayende.com/projects/rhino-mocks.aspx