Why is field injection causing class difficult to experience?

According to Google I / O 2009 - Big modular Java with Guice (at 29:55) using field injection makes testing harder than constructor or setter injection.

Consider the NewsService class:

public class NewsService{

    @Inject Storage storage;
    @Inject Authenticator auth;

    public void addNews(String data){
       if(auth.authenticate()){
          storage.save(data);
       }
    }
}

and unit test for NewsService:

@RunWith(MockitoJunitRunner.class)
public class NewsServiceTest{

    @Mock Storage storage;
    @Mock Authenticator auth;
    @InjectMocks NewsService sut; //system under test

    @Test
    public void addNews_ShouldCallSaveOnStorage_WhenAuthIsSucceed(){
       when(auth.authenticate()).thenReturn(true);
       sut.addNews("mocked data");
       verify(storage).save("mocked data");
    }
}

Mockito . , . Storage Authenticator , NewsService, - , , NewsService Storage Authenticator, behaoviour.

NewsService , unit test .

, : ?

+4
1

, ; , , mock- new. .

, , . , ( , ) , @Inject Java. .

+3

All Articles