As I continue my efforts to get my Android applications under unit and integration tests, I have come across a few tips/tricks to successfully deal with troublesome code. So I decided to start a new blog post series, “Testing Tricks”. I will try to post a new trick each week. So let’s get started…
Problem Code
public void readDeepLink(String path) { new DeepLinkReader().readDeepLink(path); }
Solution
Fixed Code
// in the MainPresenter.java public void readDeepLink(String path) { getDeepLinkReader().readDeepLink(path); } DeepLinkReader getDeepLinkReader() { return new DeepLinkReader(currentData, events); }
// in the MainPresenterTest.java @Mock private DeepLinkReader deepLinkReader; @Test public void shouldReadDeepLink() throws Exception { MainPresenter mainPresenter = new MainPresenter() { DeepLinkReader getDeepLinkReader() { return deepLinkReader; } }; mainPresenter.readDeepLink("washingtonpost.com"); verify(deepLinkReader).readDeepLink("washingtonpost.com"); }
Hope you found this useful, until next time!