Introduction
Guit provides several testing utilities based on JUnit4 and guice. These utilities do not depend on any mocking framework, so you can use the one you like the most.
Still, GuitTest can mock blind interfaces.
Command service and GuitTest
All calls to the command service will work as you expect in a guit test. When you configure your test you need to bind/mock your server side dependencies too.
GuitTest
A GuitTest lets you declare or mock your dependencies and get an instance of a presenter with one line of code to start testing.
Example (from Contacts demo)
/**
* Guit test for ContactsPresenter.
*/
public class TestContactsPresenter extends GuitTest {
@Override
protected void configure() {
mock(ContactsView.class);
mock(ContactsBinder.class);
mock(HasContent.class);
bind(DataUtil.class).in(Singleton.class);
}
@Inject
ContactsPresenter presenter;
@Before
public void setUp() {
presenter.go(null);
}
@Test
public void testSelect() {
// Select two
presenter.contactsTable$click(Actions.SELECT, 0);
presenter.contactsTable$click(Actions.SELECT, 1);
Assert.assertEquals(2, presenter.selectionModel.getSelectedItems().size());
// Unselect one
presenter.contactsTable$click(Actions.SELECT, 1);
Assert.assertEquals(1, presenter.selectionModel.getSelectedItems().size());
// Select three more
presenter.contactsTable$click(Actions.SELECT, 4);
presenter.contactsTable$click(Actions.SELECT, 5);
presenter.contactsTable$click(Actions.SELECT, 6);
Assert.assertEquals(4, presenter.selectionModel.getSelectedItems().size());
}
@Test
public void testDelete() {
presenter.contactsTable$click(Actions.SELECT, 0);
presenter.contactsTable$click(Actions.SELECT, 1);
int oldSize = presenter.contactDetails.size();
presenter.deleteButton$click();
// Yes...I know this isn't the best assert ever :)
Assert.assertEquals(oldSize - 2, presenter.contactDetails.size());
}
@Test
public void testEdit() {
eventBus.addHandler(new PlaceGoHandler() {
@Override
public void onPlaceGo(PlaceGoEvent event) {
Assert.assertEquals(EditContactPresenter.class, event.getPlaceClass());
Assert.assertEquals(presenter.contactDetails.get(2).getId(), event.getPlaceData());
}
}, PlaceGoEvent.getType());
presenter.contactsTable$click(Actions.CLICK, 2);
}
}Jre tests and gwt compiler alerts
When testing your presenters its natural to put your tests in the same package than the presenter.
GuitTests use classes than aren't available for the gwt's compiler, and because those classes are in a 'client' package the compiler will log exceptions while compiling.
Those exceptions aren't a problematic, the compiler will finish successfully, but still, they are annoying.
How to organize your test code to avoid error logs
To organize your tests you can separate your project in 3 source folders:
- src: your project code
- test: GuitTests
- gwttest: GwtTestCase
To avoid errors on development mode get into the luncher and remove the "test" folder from your classpath.
Avoid the errors while compiling it is a bit harder. You cannot use the "Compile" button of the eclipse plugin. You need to create a luncher for the compiler and exclude the "test" folder from the classpath. Steps to do it:
- Go to "Run configurations"
- Go to "Java Application"
- Create a new luncher
- Select your Project
- In "Main class" search for: "com.google.gwt.dev.Compiler"
- Go to the "Classpath" tab and add: "src" and any other you need (do not add "test")
- Go to your development mode luncher (under Web Application) -> Arguments -> copy "Program arguments" text
- Go to you compiler luncher -> Arguments -> paste the arguments in "Program arguments" and then remove all arguments but:
- -logLevel
- -gen (if you had it)
- -war
- The last part is the modules, so leave them
Then just run your luncher and it will compile without exceptions.
A good side of using a luncher is that you can stop your compiler.