Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Presenter unit test with RxJava CompositeSubscription

I would like to create a test for my Presenter class but I'm having problems with the CompositeSubscription instance inside the Presenter itself. When I run the test I'm getting 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();
    //creation methods...

    public void addSubscription(Subscription subscription) {
       if (compositeSubscription == null || compositeSubscription.isUnsubscribed()) {
           compositeSubscription = new CompositeSubscription();
         }
       compositeSubscription.add(subscription);
    }

    public void getGummyBears() {
        addSubscription(coreModule.getGummyBears());
    }
}

The CoreModule is an interface (part of a different module) and there is another class CoreModuleImpl in which are located all retrofit API calls and their conversion to Subscriptions. Something like:

@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
}

Now what I want to do is to test the getGummyBears() method. My test method looks like this:

@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);
}

I already saw many test examples from different projects but no one is using this Subscription approach. They just return the Observable which is consumed directly inside the presenter. And in that case I know how a test has to be written.

What's the correct way to test my situation?

like image 819
Niccolò Passolunghi Avatar asked Nov 09 '22 08:11

Niccolò Passolunghi


1 Answers

Looks like coreModule.getGummyBears() is returning null. Just step through with debug and it should be pretty clear. When using mocking frameworks you can get null returned from method calls on a mocked object when you haven't specified what the method call should return on that mocked object.

like image 175
Dave Moten Avatar answered Nov 14 '22 21:11

Dave Moten