Refactoring in order to write "pretty" JUnit tests

java, mocking, mockito, refactoring, tdd

Solution

Your class should have the `WebService` object (I refuse to call it a 'stub') as a field.

class Someclass{

  @Resource
  private WebService ws;

  public void sendMessage(){

  ws.sendMsg();        
  }
}

It should be injected with a DI framework of your choice. In your test, you can set it to a mock. There's no need for a lazy getter, as your point out that's not thread-safe.

Problem

I want to clarify refactoring in scope 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 verify sendMsg() method and make some asserts with result. To have posibility to mock this stub i move this stub local variable to instance variable. So that I can set mocked stub to class and do verivyings and asserts in test class. For example: ``` @Test public void testSMth(){ wsProvider.setStub(stubMock); verify(stubMock).sendMsg(); ...asserts } ``` This approach is not thread safety and I should do some concurrency modification. This modification may cause mistakes. So in local variable approce there is thread safty. Also i could create Factory that will return an instance of WebServiceStub. But this approache will produce new classes because this situation is frequent. There is a question: how test this cases and does goot test cost modification that may cause mistakes?

Original source