Can UI components be tested through JUnit/GWTTestCase?

134 views
Skip to first unread message

King_V

unread,
Apr 24, 2012, 4:50:45 PM4/24/12
to Google Web Toolkit
Ok, I found some old messages (several years old) that says this
cannot be done.

However, when I look at:
https://developers.google.com/web-toolkit/doc/latest/DevGuideTesting#DevGuideJUnitSetUp

this seems to imply that you can actually test GUI items on a browser
with such tests. I might well be completely misinterpreting what it's
there for, though.

However, I'm baffled as to how to do so.

For example, I might normally put MyWidget into the RootPanel as
follows:

RootPanel.get().add(new MyWidget("Some text here"));


But how would I do this in a JUnit test case / GWTTestCase?

Thanks.

Thomas Broyer

unread,
Apr 25, 2012, 4:46:53 AM4/25/12
to google-we...@googlegroups.com

Joseph Lust

unread,
Apr 25, 2012, 6:06:05 PM4/25/12
to google-we...@googlegroups.com
Keep in mind that GWTTestCase is running in HtmlUnit, which is essentially a headless browser. You can test some stuff with DOM calls, but HtmlUnit is based (last I checked) on FF3.5, so many newer things like SVG cannot be tested with it.

For full bore tests I suggest the Selenium, Watir, or Cucumber testing frameworks. I prefer Cucumber-JVM since you can write them as JUnit tests.

Sincerely,
Joseph

Clint Gilbert

unread,
Apr 24, 2012, 8:28:17 PM4/24/12
to google-we...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I've been able to test Widgets with JUnit in a project I'm working on.
I'm able to make widgets, see that they respond to external events
(notifications about state they observe, or event types they're
subscribed to via an EventBus, that sort of thing), and even send UI
events to them:

final MyWidget myWidget = new MyWidget(...);

myWidget.someButton.fireEvent(new ClickEvent() { });

//assert that the right things happened

The anonymous subclass of ClickEvent feels a little hacky, but it works
for us because our widgets (for the most part) just care that they got
clicked, they don't care about x and y coords, etc etc.

Writing the tests was relatively straightforward, but getting them to
run as part of a Maven build took some doing. A pom with all the
necessary incantations is here:

https://open.med.harvard.edu/svn/shrine/trunk/code/webclient/pom.xml

I don't know if that's minimal or not; I started with what was generated
by the gwt-maven-plugin, and tweaked the pom until it did what I wanted.
:/ We use a TestSuite (ugh) and follow the naming conventions for test
classes and suites recommended by the gwt-maven-plugin docs.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iEYEARECAAYFAk+XRSEACgkQ0GFaTS4nYxviVACeMqLdXljCkWI3lrS9tCDBulvF
pqkAn24TZuVGsdOccZTBDCWlGsGjpMi/
=G4pS
-----END PGP SIGNATURE-----

Reply all
Reply to author
Forward
0 new messages