com.googlecode.gwt.test.guice.GwtGuiceTest Maven / Gradle / Ivy
package com.googlecode.gwt.test.guice;
import org.junit.Before;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.googlecode.gwt.test.GwtTest;
import com.googlecode.gwt.test.exceptions.GwtTestConfigurationException;
import com.googlecode.gwt.test.rpc.RemoteServiceCreateHandler;
/**
*
* Base class for tests that needs to be run with a backend configured with Google Guice.
*
*
* You can inject any object in subclasses. For {@link RemoteService} objects, see the
* {@link GwtGuiceTest#getRpcServiceFromInjector(Injector, Class, String)} method which you might
* want to override to get deferred binding working as you need in test mode.
*
*
*
* The {@link GwtGuiceTest#getInjector()} also provides a default way to get a working Guice
* {@link Injector} for your test.
*
*
* @author Alex Dobjanschi
* @author Gael Lazzari
*
*/
public abstract class GwtGuiceTest extends GwtTest {
/**
* Guice test initialization method.
*/
@Before
public void beforeGwtGuiceTest() {
final Injector injector = getInjector();
// add a new RemoteServiceHandler which will call Guice Injector
addGwtCreateHandler(new RemoteServiceCreateHandler() {
@Override
protected Object findService(Class remoteServiceClass, String remoteServiceRelativePath) {
return getRpcServiceFromInjector(injector, remoteServiceClass,
remoteServiceRelativePath);
}
});
injector.injectMembers(this);
}
/**
* Get the Guice injector to use. This implementation search in web.xml for any listener
* assignable to {@link GuiceServletContextListener} to retrieve its internal Injector by
* introspection.
*
* @return The Guice Injector to use.
*/
protected Injector getInjector() {
Injector injector = GwtGuiceHelper.get().getInjector();
if (injector == null) {
throw new GwtTestConfigurationException(
this.getClass().getSimpleName()
+ ".getInjector() default implementation is not able to localize the Guice Injector to use. You should override it to provide our own implementation");
}
return injector;
}
/**
* Retrieve a GWT RPC service from a Guice injector. This implementation reads ServletDefinition
* declared in a Guice {@link ServletModule} to be able to bind object through the supplied
* remote service relative path of the targeted servlet.
*
* @param injector The Guice Injector configured.
* @param remoteServiceClass The remote service interface of the Guice object to retrieve.
* @param remoteServiceRelativePath The remote service relative path of the Guice object to
* retrieve.
* @return The corresponding Guice object, or null if no object has been found for this type and
* path.
*
* @see GwtGuiceTest#getInjector()
*/
protected Object getRpcServiceFromInjector(Injector injector, Class remoteServiceClass,
String remoteServiceRelativePath) {
return GwtGuiceHelper.get().getRpcServiceFromInjector(injector, remoteServiceClass,
remoteServiceRelativePath);
}
}