View Full Version : Render components in JUnit tests

20 Sep 2011, 9:05 AM

I'm using GWT's JUnit test cases to test my user interface. I have a GXT composite which contains two toggle buttons, and they are in the same toggle group. The toggle buttons are for selecting a person's gender, male or female.

So my unit test consists in instantiating the widget, accessing the widget and calling its method toggle().

This is my test:

PersonForm form = new PersonForm();

And my test fails because in the end, both buttons are pressed.

I have looked inside the source code for GXT's ToggleButton, and a button will only untoggle the other buttons when it was rendered. This means that buttons are not rendered inside my JUnit test. Note that in line 2, I tried to render() my widget inside the RootPanel of the application, but that did not cause the internal widgets to be rendered.

How can I create new instances of components inside a GWT test case, and make sure they are rendered ?

Colin Alworth
21 Sep 2011, 3:35 PM
Instead of directly invoking render, do what you would do in a regular app - add it to the RootPanel directly. Render only causes the dom elements to be created, and doesn't manage the rest of the widget lifecycle.