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 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(); //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 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(); //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 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 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 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?

+5
source share
2 answers

It looks like coreModule.getGummyBears() returns null. Just go to debugging, and that should be pretty clear. When using mocking frameworks, you can get null returned from method calls on the mocked object when you did not specify what the method call should return on this mocked object.

+1
source

As Dave noted, you need to mock the return value of CoreModule.getGummyBears . It is strange that you are not using the created CoreModuleImpl . Instead, you pass the coreModule to the presenter constructor.

You can make fun of getGummyBears() by doing something like this:

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

Then you need to eliminate the specific error that you encounter. This is not like you need CoreModuleImpl for this particular test case.

0
source

All Articles