mardi 26 avril 2016

UntTest - Mock RxJava object in UseCase Object

I want to test UseCase object, in this specific case there is a LoginUseCase, which looks like this:

public class LoginUseCase implements RxUseCase<AuthResponse, AuthCredentials> {

    ApiManager mApiManager;

    public LoginUseCase(ApiManager apiManager) {
        mApiManager =apiManager;
    }

    @Override
    public Observable<AuthResponse> execute(final AuthCredentials authCredentials) {
        return Observable.just(1)
                .delay(750, TimeUnit.MILLISECONDS)
                .flatMap(l -> mApiManager.login(authCredentials.getLogin(), authCredentials.getPassword()));
    }
}

I wrote simple test:

@RunWith(MockitoJUnitRunner.class)
public class LoginUseCaseTest {

    private LoginUseCase mLoginUseCase;
    @Mock ApiManager mApiManager;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mLoginUseCase = new LoginUseCase(mApiManager);
    }


    @Test
    public void testShouldThrowsError() throws Exception {
        TestSubscriber<AuthResponse> testSubscriber = new TestSubscriber<>();
        doReturn(Observable.error(new Throwable())).when(mApiManager).login("", "");
        mLoginUseCase
                .execute(new AuthCredentials("correct", "credentials"))
                .subscribe(testSubscriber);
        testSubscriber.assertNoErrors();

    }
}

But this test always passes and I don't know how mock error observable in this case.

Aucun commentaire:

Enregistrer un commentaire