Presenterクラスのテストを作成したいが、Presenter自体のCompositeSubscriptionインスタンスに問題がある。そこ RxJava CompositeSubscriptionによるプレゼンターユニットテスト
public class Presenter {
CompositeSubscription compositeSubscription = new CompositeSubscription();
//creation methods...
public void addSubscription(Subscription subscription) {
if (compositeSubscription == null || compositeSubscription.isUnsubscribed()) {
compositeSubscription = new CompositeSubscription();
}
compositeSubscription.add(subscription);
}
public void getGummyBears() {
addSubscription(coreModule.getGummyBears());
}
}
がCoreModuleは、インターフェース(異なるモジュールの一部)であると:
java.lang.NullPointerException
at rx.subscriptions.CompositeSubscription.add(CompositeSubscription.java:60)
at com.example.Presenter.addSubscription(Presenter.java:67)
at com.example.Presenter.getGummyBears(Presenter.java:62)
これは大体私のプレゼンタークラスです:私は、テストを実行すると、私はこのエラーを取得していますCoreModuleImplの別のクラスであり、すべてのAPI呼び出しの更新とサブスクリプションへの変換が行われます。以下のような 何か:
@Override public Subscription getGummyBears() {
Observable<GummyBears> observable = api.getGummyBears();
//a bunch of flatMap, map and other RxJava methods
return observable.subscribe(getDefaultSubscriber(GummyBear.class));
//FYI the getDefaultSubscriber method posts a GummyBear event on EventBus
}
は今、私が何をしたいのかgetGummyBears()
メソッドをテストすることです。私はすでに別のプロジェクトから多くのテストの例を見た誰もが、このサブスクリプションのアプローチを使用していない
@Mock EventBus eventBus;
@Mock CoreModule coreModule;
@InjectMock CoreModuleImpl coreModuleImpl;
private Presenter presenter;
@Before
public void setUp() {
presenter = new Presenter(coreModule, eventBus);
coreModuleImpl = new CoreModuleImpl(...);
}
@Test
public void testGetGummyBears() {
List<GummyBears> gummyBears = MockBuilder.newGummyBearList(30);
//I don't know how to set correctly the coreModule subscription and I'm trying to debug the whole CoreModuleImpl but there are too much stuff to Mock and I always end to the NullPointerException
presenter.getGummyBears(); //I'm getting the "null subscription" error here
gummyBears.setCode(200);
presenter.onEventMainThread(gummyBears);
verify(gummyBearsView).setGummyBears(gummyBears);
}
: 私の試験方法は次のようになります。彼らは、Observableを返します。Observableは、プレゼンターの中で直接消費されます。その場合、私はテストをどのように書かなければならないかを知っています。
私の状況をテストする正しい方法は何ですか?
あなたはコンストラクタで 'CoreModule'を取りますか? – skywall
うわー、申し訳ありません 'setUp'メソッドの' presenter = new Presenter(coreModule、eventBus) 'を追加するのを忘れました – nicopasso
こんにちは@nicopassoそれを解決する方法はありますか? –