I would like to create a test for my Presenter class, but I'm having problems with the CompositeSubscription instance inside Presenter itself. When I run the test, I get this error:
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)
This is roughly my Presenter class:
public class Presenter { CompositeSubscription compositeSubscription = new CompositeSubscription();
CoreModule is an interface (part of another module), and there is another CoreModuleImpl class that contains all modified API calls and their conversion to Subscriptions. Sort of:
@Override public Subscription getGummyBears() { Observable<GummyBears> observable = api.getGummyBears();
Now I want to test the getGummyBears() method. My testing method is as follows:
@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 have already seen many test cases from different projects, but no one uses this subscription approach. They simply return the Observable, which is consumed directly in the presenter. And in this case, I know how the test should be written.
What is the right way to test my situation?
source share