REST Client Testing With MockRestServiceServer
January 9th, 2013
Using Spring MockRestServiceServer for RestTemplate client functional and unit testing, with code samples and examples.
One of the oft-touted benefits of Grails is that you can automatically generate a user interface for basic database operations such as creating a new entity, updating, deleting, and listing all entities.
While this looks really neat in demos, in most cases, you don’t want to use the scaffolded files as your real application’s production user interface. If you are editing a simple code table, such as a list of countries or states, then the scaffolded UI may be sufficient. In most cases, though, it won’t. Furthermore, scaffolded controller code puts the database operations directly in the controller, not in a transaction service where such code belongs.
The scaffolding is still useful especially during the early phases of a project, where the ability to generate a basic user interface from domain classes can really speed up your development. Recent versions of Grails have improved the generated interface, adding in HTML 5 features (of mixed blessing, alas) and a much, much better look. Even so, I have never used the Grails-generated styles as is in any real project. I’ve always had to adopt a look and feel for a particular company or application and that look and feel has rarely been close to the Grails interface.
That said, having a default user interface really helps as you flesh out the application. It provides a set of code that you can then customize and normally speeds up your work. And, this gets even better if you can generate a basic interface that comes closer to the final interface you want.
Grails has allowed you to customize the scaffolded templates for a long time. That is, you can customize how Grails generates a user interface from your domain classes, putting in the styles you want, the form layout you want, and the customized HTML you want. But that has been a sort of all or nothing approach, having to edit complex, daunting templates to get anything done. To help with this, you can use the fields plugin.
Described in one of the best talks at the 2012 GR8 US Conference, the fields plugin allows you to easily customize the scaffolded user interface in small chunks. You can customize one small piece without having to mess with the whole thing. With this, you can apply your organization’s look and feel directly when generating views. Even if you don’t use this code directly, it means you will have a lot less to customize to get to your final result.
To use the fields plugin you need to:
After that, you can customize the templates to get closer and closer to the real user interface you desire.
Go to http://www.grails.org/plugin/fields to see the details of the fields plugin. You can usually simply add the following line to your BuildConfig to include this plugin into your application:
`plugins {
// …
compile “:fields:1.3”
}
`
Obviously, change the version number when new versions of the fields plugin get released.
To download the plugin locally into your application, run a Grails command such as compile:
grails compile
Run the list-plugins command to confirm that the fields plugin is installed in your application:
grails list-plugins
Once the plugin is downloaded and installed into your application, install the plugin templates with the following command:
grails install-form-fields-templates
This command installs the fields plugin templates into src/templates/scaffolding/
.
Take a look at these files. You’ll see individual templates for the common GSPs of create, edit, list, and show.
With the fields plugin, you can customize these four main templates (create, edit, list, and show), or you can customize individual elements, such as how to display dates or how to align elements in a form. There are a host of other overrides you can define, all documented very nicely at http://freeside.co/grails-fields/.
I also recommend looking over Rob Fletcher’s slides at https://github.com/sjurgemeyer/GR8-US-2012. And browse the demo code at https://github.com/robfletcher/fields-gr8conf-demo/. You’ll find a lot of information to jump start your work with the fields plugin.
Dynamic vs. Static Scaffolding
With Grails scaffolding, you normally have two choices: dynamic scaffolding creates the views on the fly from the current values in the domain class. Generated scaffolding (using the grails generate-all or grails generate-views commands) creates static views as a snapshot in time for whatever fields were in the domain class when you generated the views.
With the fields plugin, you’ll find even the statically-generated files become more dynamic. By default, the fields plugin puts in the f:all
tag to generate an entire form:
<f:all bean="${propertyName}"/>
The f:all
tag generates your form dynamically, one field at a time. The GSP then contains the HTML and tags that go before and after your form, such as displaying the list of errors as well as the submit button. The variable propertyName
gets defined at scaffolding time, that is, when you create the views.
Making Nicer Views The fields plugin really appealed to me because of how easy it was to create much better-looking views. At the 2012 GR8Conf US session, Rob Fletcher showed a number of different user interface styles, including a very nice example of using the styles from Twitter Bootstrap.
Twitter Bootstrap, at http://twitter.github.com/bootstrap/, provides a very nice set of CSS styles to help create good-looking Web pages, along with excellent documentation. To use Twitter Bootstrap in your Grails applications, you can install one of the Grails plugins based on Bootstrap, or just download and install bootstrap.css
from the Twitter Bootstrap site.
You will probably want to create a Grails layout that uses the Bootstrap row or row-fluid classes to place the basic elements of your interface, such as headers, footers, and navigation areas. You’ll also want to add your organization’s look and feel here, defining the fonts, colors, and placements needed for your application.
Once you are using the bootstrap.css
file, you can set up the scaffolded forms to use the Bootstrap style for form layout. To do this:
create.gsp
and edit.gsp
templates in the src/templates/scaffolding/
directory).In addition to the form-horizontal class in the create.gsp
and edit.gsp
templates, you probably also want to make use of the Bootstrap styles for buttons, such as btn
(the basic class) and btn-primary
or btn-success
for a nice rounded-corner gradient button style on the submit buttons.
The fields plugin lays out each property on the form using the f:field
tag. You can customize this tag’s template by creating the file views/_fields/default/_field.gsp
and defining your own way to lay out form elements. Luckily, one of the examples from the talk does exactly that. Look at the branches in the demo code at https://github.com/robfletcher/fields-gr8conf-demo/. One of the branches shows how to integrate Twitter Bootstrap and shows what to put into the _field.gsp
file:
`<%@ page defaultCodec=“html” %>
Using Spring MockRestServiceServer for RestTemplate client functional and unit testing, with code samples and examples.
Using Ant Paths to enhance the Grails Resources plugin while organizing the code into modules with a common pattern.
Some impressions after using the new Eclipse Juno IDE for a few days.
Eric has decades of industry experience in designing and developing complex enterprise software, including designing and developing Grails and Java EE solutions to tough client problems. He has experience leading development teams, mentoring developers, and helping troublesome projects get back onto a success track. He has lead teams in both traditional and agile settings.