Skip to content

Instantly share code, notes, and snippets.

@mgenov
Created January 19, 2011 08:12
Show Gist options
  • Save mgenov/785859 to your computer and use it in GitHub Desktop.
Save mgenov/785859 to your computer and use it in GitHub Desktop.
RequestFactory Test Sample
package com.clouway.testing;
import com.clouway.server.PricingModule;
import com.clouway.server.inject.InjectingServiceLayerDecorator;
import com.clouway.server.inject.RequestFactoryInjectingModule;
import com.clouway.shared.service.PricingRequestFactory;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.requestfactory.server.ServiceLayer;
import com.google.gwt.requestfactory.server.SimpleRequestProcessor;
import com.google.gwt.requestfactory.server.testing.InProcessRequestTransport;
import com.google.gwt.requestfactory.server.testing.RequestFactoryMagic;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* TestingHelper is a testing helper class whose purpose is to simplify testing of the integration of UI and Persistence layer.
*
*
* Sample usage:
* <pre>
*
* public class CustomerEditorTest {
* TestingHelper helper = new TestingHelper(this);
*
* {@literal@}Inject
* PricingRequestFactory pricingRequestFactory;
*
* {@literal@}Inject
* EventBus eventBus;
*
*
* {@literal@}Before
* public initialize() {
* helper.setUp();
* }
*
* // test methods are comming here
* }
}
* </pre>
*
* //TODO: Think how the persistence helper could be used for cleaning of the persistence state.
* //TODO: Add additional helper classes which are responsible for the private domain management such as (cache, persistence and etc)
*
* @author Miroslav Genov (mgenov@gmail.com)
*/
public class TestingHelper {
private PricingRequestFactory rf;
private SimpleEventBus eventBus;
private Object targetTest;
public TestingHelper() {
}
/**
* Creates a new testing helper by specifying the targetTest case which is using it. When targetTest is specified then
* the services could be injected into it.
* @param targetTest
*/
public TestingHelper(Object targetTest) {
this.targetTest = targetTest;
}
/**
* Initializes testing environment dependencies by using Guice, so the dependencies could be injected
* directly into the target class.
*
*/
public void setUp() {
eventBus = new SimpleEventBus();
rf = RequestFactoryMagic.create(PricingRequestFactory.class);
Injector injector = Guice.createInjector(
new RequestFactoryInjectingModule("/gwtRequest"),
new PricingModule(),
new AbstractModule() {
@Override
protected void configure() {
bind(EventBus.class).toInstance(eventBus);
bind(PricingRequestFactory.class).toInstance(rf);
}
}
);
InjectingServiceLayerDecorator decorator = injector.getInstance(InjectingServiceLayerDecorator.class);
// we had specified target test, which means that we had to inject
// all requested dependencies into it.
if (targetTest != null) {
injector.injectMembers(targetTest);
}
rf.initialize(eventBus, new InProcessRequestTransport(new SimpleRequestProcessor(ServiceLayer.create(decorator))));
}
public void tearDown() {
//TODO: Clean up persistence store after test was finished (in case real persistence datastore is used)
}
public EventBus getEventBus() {
return eventBus;
}
public PricingRequestFactory getRequestFactory() {
return rf;
}
}
public class UpdateTemplateDiscountsTest {
@Inject
PricingRequestFactory rf;
@Inject
EventBus eventBus;
TestingHelper helper = new TestingHelper(this);
@Before
public void initializeRequestFactory() {
helper.setUp();
}
@Test
public void testLoadTemplate() {
EditTemplateView view = createMock(EditTemplateView.class);
EditTemplateActivity activity = new EditTemplateActivity(rf,view,new EditTemplatePlace("1"));
view.setTemplateName("Template1");
view.setPresenter(activity);
view.removeExistingDiscounts();
view.populatePriceDiscounts(isA(List.class));
replay(view);
activity.start(new MockAcceptsOneWidget(), eventBus);
verify(view);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment