4

I am very new to dagger--I don't even know yet if it will work for my application

I have a search page that returns the latest news about a given celebrity.

I have written a test to verify that results appear on the page when we search for a popular celebrity.

The page has a searchField, which requires page in its constructor so the web driver I use for my tests can select it.

Celebrity Search Page Test

public class CelebritySearchPageTest {
    @Test
    public void testSearchResultsForKevinBaconVerifyHisPopularity() {
        CelebritySearchPage searchPage = new CelebritySearchPage();
        searchPage.searchFor("Kevin Bacon");
        Assert.assertTrue(searchPage.getNumberOfResults() > 9999999, "Verify that Kevin Bacon is still relevant");
    }
}

Celebrity Search Page

public class CelebritySearchPage extends Page {
    @Inject
    @Named("search field")
    TextField searchField;

    public void searchFor(String text) {
        searchField.setText(text);
        // ...
    }

    public int getNumberOfResults() {
        // ...
    }
}

Celebrity Search Page Module

@Module(injects = CelebritySearchPage.class)
public class CelebritySearchPageModule {
    @Provides
    @Named("search field")
    public TextField provideSearchField() {
        return new TextField(/* How do I get the page? */, "#searchField");
    }
}

Page

public abstract class Page {
    // ...
}

Text Field

public class TextField {
    protected Page page;
    protected String selector;

    public TextField(Page page, String selector) {
        this.page = page;
        this.selector = selector;
    }

    public void setText(String text) {
        // ...
    }
}

The problem is that page needs searchField, but searchField needs page. How do I get over this cyclic dependency?

I can't initialize searchField inside of CelebritySearchPage

michaelsnowden
  • 6,031
  • 2
  • 38
  • 83

1 Answers1

1

Consider this:

CelebritySearchPage

public class CelebritySearchPage extends Page {
    private final Lazy<TextField> searchField;
// always prefer constructor injection
// avoid @Named if possible, since the compiler cannot check the string
    @Inject
    CelebritySearchPage(@Named("search field") Lazy<TextField> searchField) {
        this.searchField = searchField; 
    }
}

Text Field

public class TextField {
    protected final Lazy<Page> page;
    protected final String selector;

    @Inject TextField(Lazy<Page> page, String selector) {
        this.page = page;
        this.selector = selector;
    }

/*
Lazy::get()
Return the underlying value, computing the value if necessary. All calls to      the same Lazy instance will return the same result.
*/
}

I guess one Lazy should suffice as well.

Andreas Frische
  • 8,551
  • 2
  • 19
  • 27