MvpConductorLifecycleListener extends Controller.LifecycleListener { @Override public void postCreateView(@NonNull Controller controller, @NonNull View view) { MvpConductorDelegateCallback<V, P> callback = getCallback(); P presenter = callback.getPresenter(); if (presenter == null) { presenter = callback.createPresenter(); if (presenter == null) { throw new NullPointerException( "Presenter returned from createPresenter() is null in " + callback); } callback.setPresenter(presenter); } V mvpView = callback.getMvpView(); if (mvpView == null) { throw new NullPointerException("MVP View returned from getMvpView() is null in " + callback); } presenter.attachView(mvpView); } MvpConductorLifecycleListener(MvpConductorDelegateCallback<V, P> callback); @Override void postCreateView(@NonNull Controller controller, @NonNull View view); @Override void preDestroyView(@NonNull Controller controller, @NonNull View view); @Override void postDestroy(@NonNull Controller controller);  }
@Test public void createAndSetNewPresenterAndAttachView() { MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class); Controller controller = Mockito.mock(Controller.class); View view = Mockito.mock(View.class); MvpView mvpView = Mockito.mock(MvpView.class); MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback = Mockito.mock(MvpConductorDelegateCallback.class); Mockito.when(callback.getPresenter()).thenReturn(null); Mockito.when(callback.createPresenter()).thenReturn(presenter); Mockito.when(callback.getMvpView()).thenReturn(mvpView); MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener = new MvpConductorLifecycleListener<>(callback); lifecycleListener.postCreateView(controller, view); Mockito.verify(callback, Mockito.times(1)).getPresenter(); Mockito.verify(callback, Mockito.times(1)).createPresenter(); Mockito.verify(callback, Mockito.times(1)).setPresenter(presenter); Mockito.verify(presenter, Mockito.times(1)).attachView(mvpView); }
@Test public void nullPointerExceptionOnCreatePresenter() { Controller controller = Mockito.mock(Controller.class); View view = Mockito.mock(View.class); MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback = Mockito.mock(MvpConductorDelegateCallback.class); Mockito.when(callback.getPresenter()).thenReturn(null); Mockito.when(callback.createPresenter()).thenReturn(null); MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener = new MvpConductorLifecycleListener<>(callback); try { lifecycleListener.postCreateView(controller, view); Assert.fail("NullpointerException expected"); } catch (NullPointerException e) { Assert.assertEquals(e.getMessage(), "Presenter returned from createPresenter() is null in " + callback); } }
@Test public void nullPointerExceptionOnGetMvpView() { MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class); Controller controller = Mockito.mock(Controller.class); View view = Mockito.mock(View.class); MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback = Mockito.mock(MvpConductorDelegateCallback.class); Mockito.when(callback.getPresenter()).thenReturn(presenter); Mockito.when(callback.getMvpView()).thenReturn(null); MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener = new MvpConductorLifecycleListener<>(callback); try { lifecycleListener.postCreateView(controller, view); Assert.fail("NullpointerException expected"); } catch (NullPointerException e) { Assert.assertEquals(e.getMessage(), "MVP View returned from getMvpView() is null in " + callback); } }
@Test public void dontCreatePresenterIfOneExists() { MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class); Controller controller = Mockito.mock(Controller.class); View view = Mockito.mock(View.class); MvpView mvpView = Mockito.mock(MvpView.class); MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback = Mockito.mock(MvpConductorDelegateCallback.class); MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener = new MvpConductorLifecycleListener<>(callback); Mockito.when(callback.getPresenter()).thenReturn(presenter); Mockito.when(callback.getMvpView()).thenReturn(mvpView); lifecycleListener.postCreateView(controller, view); Mockito.verify(callback, Mockito.times(1)).getPresenter(); Mockito.verify(callback, Mockito.never()).createPresenter(); Mockito.verify(callback, Mockito.never()).setPresenter(presenter); }