jeudi 22 février 2018

Inject mocks into class before constructor's logic comes on

Imagine, we have a class, in which a lot of logic is being executed inside a constructor. A question - how to inject mocks in a such class before constructor's logic gets executed? Additional info: This class is not written by me and I do not want to violate existing interface in this class by changing it's structure for testability reasons. Worth mentioning, that it is a ViewModel's class instance and on it's instantiation it runs an authentication check in RxJava2 flow, using this viewModel's fields to get needed Observables. So, it is throwing NullPointerException even before @InjectMocks annotation processing comes in. If I try not to instantiate viewmodel in testing class viewmodel's field declaration, then run fails due to 'MockitoException', that is caused because of trying of Mockito itself to instantiate an object with intent to inject mocks in it after, but constructor already fails with NPE. So, it is even possible to solve this without violation of 'do not change a class for test' rule?

Aucun commentaire:

Enregistrer un commentaire