2
\$\begingroup\$

I have an interactor/usecase class in my application. My application follows MVVM architecture with interactor/usecases responsible for logic (e.g. get data from api-service, store in local-database etc).

Now I am trying to write unit tests for my interactor classes.

Here is one on those classes:

public class ChangeUserPasswordInteractor {

    private final FirebaseAuthRepositoryType firebaseAuthRepositoryType;

    public ChangeUserPasswordInteractor(FirebaseAuthRepositoryType firebaseAuthRepositoryType) {
        this.firebaseAuthRepositoryType = firebaseAuthRepositoryType;
    }

    public Completable changeUserPassword(String newPassword){
        return firebaseAuthRepositoryType.getCurrentUser()
                .flatMapCompletable(firebaseUser -> {
                        firebaseAuthRepositoryType.changeUserPassword(firebaseUser, newPassword);
                        return Completable.complete();
                })
                .observeOn(AndroidSchedulers.mainThread());
    }
}

This class changes the password for a Firebase user.

Now here is a test class I wrote:

@RunWith(JUnit4.class)
public class ChangeUserPasswordInteractorTest {

    @Mock
    FirebaseAuthRepositoryType firebaseAuthRepositoryType;
    @Mock
    FirebaseUser firebaseUser;

    @InjectMocks
    ChangeUserPasswordInteractor changeUserPasswordInteractor;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        RxAndroidPlugins.reset();
        RxAndroidPlugins.setInitMainThreadSchedulerHandler(schedulerCallable -> Schedulers.trampoline());
    }


    @Test
    public void changeUserPassword() {
        Mockito.when(firebaseAuthRepositoryType.getCurrentUser()).thenReturn(Observable.just(firebaseUser));
        Mockito.when(firebaseAuthRepositoryType.changeUserPassword(firebaseUser, "test123")).thenReturn(Completable.complete());
        changeUserPasswordInteractor.changeUserPassword("test123")
                .observeOn(Schedulers.trampoline())
                .test()
                .assertSubscribed()
                .assertNoErrors()
                .assertComplete();
    }
}

I would like some criticism and suggestions on these tests. Is this a good way to test my interactor method?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.