Skip to content

Instantly share code, notes, and snippets.

Created October 12, 2012 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3880246 to your computer and use it in GitHub Desktop.
Save anonymous/3880246 to your computer and use it in GitHub Desktop.
Creating a WicketApplication through Guice
public class HomePage extends WebPage {
@Inject
private Injector injector;
public HomePage(final PageParameters parameters) {
super(parameters);
String hello = injector.getInstance(Key.get(String.class, Names.named("hello")));
add(new Label("hello", hello));
}
}
public class TestHomePage {
private WicketTester tester;
@Before
public void setUp() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Key.get(String.class, Names.named("hello"))).toInstance("world!");
}
});
tester = new WicketTester(new WicketApplication(injector));
}
@Test
public void homepageRendersSuccessfully() {
tester.startPage(HomePage.class);
tester.assertLabel("hello", "world!");
tester.assertRenderedPage(HomePage.class);
}
}
public class WicketApplication extends WebApplication {
private final Injector injector;
@Inject
public WicketApplication(Injector injector) {
this.injector = injector;
}
@Override
public Class<HomePage> getHomePage() {
return HomePage.class;
}
@Override
public void init() {
super.init();
getComponentInstantiationListeners().add(new GuiceComponentInjector(this, injector));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment