May 2, 2018

Logical GWT client testing with Spock

GWT – Google Web Toolkit may not be as heavily used as it once was 5-10 years ago, though many enterprise teams and legacy projects are still using the technology. While we can easily use Spock to test our other Java and Groovy projects, and even to test some parts of GWT, the client-side portion of GWT that executes native Javascript is not quite so simple.

The GWT project does provide testing patterns for unit and integration tests, however they are a bit cumbersome to work with. The framework supplied option is to extend GWTTestCase which runs the test in an HtmlUnit headless browser, a valid but slow procedure.

Another nice option is to use GwtMockito which provides Mockito mocks for the core client-side GWT classes. This works great if you stay in a JUnit + Mockito world. But what if we would like to use Spock? It’s only logical.

Replicating all of the inner classes that are mocked out by GwtMockito would be a great thing to have for Spock, but unfortunately is not currently available. Also duplicating this effort seems inefficient for a project on its last legs.

The solution provided here leverages the goodness of using GwtMockito from within Spock (as opposed to JUnit). And really, it is fairly simple to use this pattern:

1) Setup Mocks, and Initialize GwtMockito.
– This involves normal Spock mocks for any services used by the file under test.
– GwtMockito initialization follows the pattern for usage outside of a JUnit test runner
– We also instantiate our class under test, the GwtSpockWidget, but wrapping it as a Spy

2) Spock Spy wrapper
– The Spock Spy wrapper shown above requires usage of an as-of-this-date(2018-04-30) unpublished version of Spock 1.2
– It uses a fix to allow spying instantiated objects that lack an empty constructor
– Grab it from the snapshot repo like this:

3) Simple test of @UIField element
– GwtMockito auto mocks the @UIField elements with Mockito mocks. As such, we need to verify them Mockito-style as seen on Line 16 below:

4) Test object under test with Spock Spy
– To verify that an action takes place somewhere else in the widget, we can easily validate an internal call through the Spy:

This was just a quick run-thru to get you started on “modern” GWT client testing with Spock and GwtMockito. It is much faster than spinning up the default GWTTestCase container, you don’t have to stumble through JUnit assertions, and best of all it uses Spock. Precisely.

Full test source:

About the Author

Jeff Sheets profile.

Jeff Sheets

VP - Technology

Jeff has developed Java, Groovy, Grails, and Javascript web apps for industries as varied as Defense, Energy, Weather, Insurance, and Telecom. He is a co-organizer of the Omaha Java Users Group. Jeff has worked on Grails projects since the Grails 1.3.x days, and has experience with production Groovy code as well as Spock tests and Gradle builds. His latest focus has been on AngularJS and Spring Boot applications using JHipster. Jeff also enjoys volunteering at local CoderDojo events to teach programming to our next generation.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Understanding Mutual TLS Options in the Public Cloud
When delivering an API over the public internet via a cloud provider, some organizations and frameworks require mutual TLS verification as a part of the interaction for that API. Mutual TLS can be used to […]
Performance Test Liquibase Update
When doing a liquibase update to a database if you’re having performance issues, it can be hard to find out which updates are causing problems. If you need to measure the time to apply each […]
TICK Stack Monitoring for the Non-Technical
TICK – Telegraf, Influx, Chronograf, and Kapacitor – is a method of monitoring your systems and applications. In this article, I discuss in non-technical terms what the difference is between TICK and Prometheus Grafana A […]
Design Systems, Part 1 • Introduction
Business leaders need a practical guide to plan and execute Design System Initiatives. The aim of this series is to be that guide. This installment introduces terms and definitions as a primer on Design Systems.