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
Using Conftest to Validate Configuration Files
Conftest is a utility within the Open Policy Agent ecosystem that helps simplify writing validation tests against configuration files. In a previous blog post, I wrote about using the Open Policy Agent utility directly to […]
SwiftGen with Image & Color Asset Catalogs
You might remember back in 2015 when iOS 9 was introduced, and we were finally given a way to manage all of our assets in one place with Asset Catalogs. A few years later, support […]
Tracking Original URL Through Authentication
If you read my other post about refreshing AWS tokens, then you probably have a use case for keeping track of the original requested resource while the user goes through authentication so you can route […]
Using Spring Beans in a Kafka Streams ExceptionHandler
There are many things to know before diving into Kafka Streams. If you haven’t already, check out these 5 things as a starting point. Bullet 2 mentions designing for exceptions. Ironically, this seems to be […]