이 보드에서 비슷한 질문을 보았지만 그 중 누구도 내 질문에 대답하지 않았습니다. 이상하게 들리 겠지만, 조롱하는 객체에서 생성자 호출을 조롱하는 것이 가능합니다.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를 사용하도록 Customer 생성자 호출을 변경할 수는 있지만 도움이 될지 확실하지 않습니다. 나는 new Customer(145)
생성자의 본문이 무엇을하는지에 대한 제어권이 없습니다.
이것이 가능합니까?
생성자에서 데이터베이스 연결을 설정하지 않는 것이 좋습니다. 클래스를 사용하여 클래스에 연결을 주입하십시오. –
동의. 하지만 나는 고객 생성자 논리를 제어 할 수 없다. – Setzer