I have troubles with writing a test for Single operator. For some reason Subscriber is marked as unsubscribed just after creation.
Snippet of code:
public abstract class OneShotUseCase<T> {
@NonNull
private CompositeSubscription subscription = new CompositeSubscription();
public void cancel() {
subscription.clear();
}
public void execute(@NonNull SingleSubscriber<T> subscriber) {
subscription.add(
build().subscribe(subscriber)
);
}
@NonNull
protected abstract Single<T> build();
}
Test for that:
@RunWith(JUnit4.class)
public class OneShotUseCaseTest {
private OneShotUseCase<String> sut;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
sut = new TestUseCase();
}
@Test
public void shouldBuildUseCase() {
final TestSingleSubscriber<String> stringTestSingleSubscriber = new TestSingleSubscriber<>();
sut.execute(stringTestSingleSubscriber);
stringTestSingleSubscriber.assertValue("Test value");
}
@Test
public void shouldCancelUseCase() {
final TestSingleSubscriber<String> stringTestSingleSubscriber = new TestSingleSubscriber<>();
sut.execute(stringTestSingleSubscriber);
sut.cancel();
stringTestSingleSubscriber.assertUnsubscribed();
}
private static class TestUseCase extends OneShotUseCase<String> {
@NonNull
@Override
protected Single<String> build() {
return Single.just("Test value");
}
}
}
As TestSingleSubscriber I am using following code: http://ift.tt/29xr1JY
My test fails at line stringTestSingleSubscriber.assertUnsubscribed();
I tried to dig little bit, but I found nothing before for such issue.
Is it something wrong with my understanding of RxJava Single?
Worth to mention that very similar code with Observable<T> instead of Single works in test perfectly.
I hope to find some ideas here how to make it working.
Thanks.
Aucun commentaire:
Enregistrer un commentaire