flow
flow copied to clipboard
Provide a sample unit test
I'm using mortar+flow and I want to test some transition logic in a Presenter
, so my first instinct was to mock the Flow
class and verify
.goTo()
is called appropriately. This won't easily work because I can't mock final
classes such as Flow
.
Do you have a recommendation on how to write such a test instead?
public static Flow createFlow(Object screen, Flow.Listener listener) {
return new Flow(Backstack.single(screen), listener);
}
public static class MockFlowListener implements Flow.Listener {
public Object lastScreen;
public Flow.Direction lastDirection;
@Override public void go(Backstack entries, Flow.Direction direction) {
lastDirection = direction;
lastScreen = entries.current().getScreen();
}
}
private Presenter presenter;
private MockFlowListener listener = new MockFlowListener();
@Before public void setUp() {
presenter = new Presenter(createFlow(new MyScreen(), listener));
}
@Test public void onTryAgainClicked() {
presenter.onTryAgainClicked();
assertThat(listener.lastScreen).isInstanceOf(SomeOtherScreen.class);
}
Actually, let's leave this open to remember to make a proper sample.
Closing. Flow is not mockable, but Dispatcher can be mocked or stubbed. Making Flow non-final or an interface just isn't practical while the API is in such flux.
Thus isn't about making flow non-final, it's about demonstrating how to unit test it.