我看了一下這個主板上的類似問題,但沒有人回答我的問題。這聽起來很奇怪,但是可以在你嘲笑的對象上模擬一個構造函數調用。EasyMock:在java中構造一個構造函數調用
例子:
class RealGuy {
....
public void someMethod(Customer customer) {
Customer customer = new Customer(145);
}
}
class MyUnitTest() {
public Customer customerMock = createMock(Customer.class)
public void test1() {
//i can inject the mock object, but it's still calling the constuctor
realGuyobj.someMethod(customerMock);
//the constructor call for constructor makes database connections, and such.
}
}
我怎麼能指望一個構造函數調用?我可以改變客戶構造函數調用使用newInstance,但我不知道這是否會有所幫助。我無法控制new Customer(145)
構造函數的內容。
這可能嗎?
在構造函數中不建立數據庫連接是個好主意。使用它們將連接注入到類中。 –
同意。但是我沒有控制這個Customer構造函數的邏輯。 – Setzer