2016-09-27 3 views
5

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は、プレゼンターの中で直接消費されます。その場合、私はテストをどのように書かなければならないかを知っています。

私の状況をテストする正しい方法は何ですか?

+0

あなたはコンストラクタで 'CoreModule'を取りますか? – skywall

+0

うわー、申し訳ありません 'setUp'メソッドの' presenter = new Presenter(coreModule、eventBus) 'を追加するのを忘れました – nicopasso

+0

こんにちは@nicopassoそれを解決する方法はありますか? –

答えて

1

coreModule.getGummyBears()がnullを返すように見えます。デバッグを実行するだけで、かなり明確になるはずです。モッキング・フレームワークを使用する場合、そのモックされたオブジェクトでメソッド・コールが返すものを指定していない場合、モックされたオブジェクトのメソッド・コールからnullを返すことができます。

+0

実際、コードは私が質問で書いたものよりもはるかに複雑です。 「coreModule」クラスはインタフェースであり、このインタフェースを実装する別のクラス「coreImpl」があります。私は私のプロジェクトの構造に従って質問を更新しました。 – nicopasso

0

Daveが述べたように、戻り値CoreModule.getGummyBearsをモックする必要があります。奇妙なことの1つは、作成中のCoreModuleImplを使用していないことです。代わりに、coreModuleをプレゼンターのコンストラクターに渡しています。

あなたはこのような何かを行うことによってgetGummyBears()を模擬することができます

when(coreModule.getGummyBears()).thenReturn(MockBuilder.newGummyBearList(30); 

は、その後、あなたが発生している特定のエラーが解決されなければなりません。この特定のテストケースでは CoreModuleImplが必要なようには見えません。