是否可以在构造函数中使用模拟?
Class A{
public B b = new B();
public A( String input ){
//I need to stub this method
b.someMethod( input );
}
// Class implementations
}
单元测试:
Class ATest{
@Mock
B b;
@InjectMock
A a;
//option1:
@Before
setup(){
MockitoAnnotations.initMocks( this ); //Fails - since A isnt instantiated
a = new A();
}
//option2:
@Before
setup(){
a = new A();
MockitoAnnotations.initMocks( this ); // Fails in new A() due to method i want to stub as mocks werent initialized yet !
}
}
我该如何处理?提前致谢。