I want to clarify refactoring in the area of TDD.
Before:
class Somclass{
public void sendMessage(){
WebServiceStub stub = new WebServiceStub();
...
stub.sendMsg();
}
}
After:
class Somclass{
private WebServiceStub stub;
public void sendMessage(){
...
if(stub == null){
stub = new WebServiceStub();
}
...
stub.sendMsg();
}
}
So, I want to check the sendMsg () method and make some statements with the result. To be able to mock this stub, move this local stub variable to the instance variable. So that I can configure the mocked stub to a class and do verivyings and claims in a test class. For instance:
@Test
public void testSMth(){
wsProvider.setStub(stubMock);
verify(stubMock).sendMsg();
...asserts
}
This approach is not thread safe, and I have to do some modification of concurrency. This modification may cause errors. Thus, according to the local variable, there is a safty stream.
Also I could create a Factory that will return an instance of WebServiceStub. But this approach will create new classes, because this situation is frequent.
: goot, ?