As a software developer, it is common to stick to a version of a framework for a while, and get used to working with it. Thus, when it’s time for a version upgrade, it can be somewhat of a pain in the neck.
In my case, all the previous projects I had were on Grails 2.X. and when we decided to move on to Grails3, we faced a couple issues regarding incompatibilities and differences in usage.
A difference I noted when starting with Grails 3 is its testing support. Fortunately, testing is more intensively supported now. However, that doesn’t mean there are no differences worth going over or new features to be explained.
The first new thing I found when it was time for Grails3 Testing was that functional testing is now supported, allowing us to easily test our running application, with a server running and full access to GORM and the database.
In previous versions, functional testing was possible, but it was necessary to put together a couple of pieces: A supporting testing framework such as JUnit, a web browser automation tool like Selenium or WebDriver and a plugin to automate functional tests, which could for instance be Geb.
Nowadays, it is possible to create and execute functional tests by simply issuing grails commands. This is due to the fact that Geb+Selenium have been incorporated to the framework and they also are integrated with Spock, allowing us to easily create more declarative spec-style functional tests.
To create functional tests, you now just type the command:
grails create-functional-test NameOfTest
This will create a test file NameOfTestSpec.groovy which will contain the tests to be run in functional mode.
Tests are created using the same Spec notation long used for unit testing, based on the Spock framework. Once written and ready, they can be run by typing:
grails test-app -functional
This will set up a server instance and deploy the app using the testing environment configuration and run the tests against it, which is the default configuration expected for testing. However, if you usually run tests with a custom configuration, you might want to have a look at the following section.
One significant change in Grails 3 concerns the directory and file structure. Many files have been relocated and some other added. Detailed location mapping can be found at https://grails.github.io/grails-doc/latest/guide/upgrading.html.
Regarding environments and configurations, the new file distribution has a considerable impact in everyday work, and therefore in testing too.
Config.groovy has mutated into application.yml. It is posible to keep a file application.groovy with the contents of Config.groovy if you feel uncomfortable with yml, but it is recommended to use the new format, which is really easy to learn and understand if you have dealt with other markup key-value style languages.
The interesting part comes when trying to configure separate environments for running the app. One way to do that, is to write different environment entries for each one in application.yml the same way you would have done in Grails 2.x.
Nevertheless, such a configuration is of no use in some cases, such as when each developer in a concurrent development process needs to specify different parameters for his own local machine. That’s why Grails 2.X gave us the possibility to override the common configuration with a local file by means of adding its path to the “locations” property in Config.groovy.
In Grails 3, it is possible to get the same output by setting up a separate file with the needed environment configuration. Doing this is as easy as just creating a file named “application-myEnv.yml” and adding your custom configuration parameters there, as if it were application.yml.
Such a file defines a new environment “myEnv”, which can be used when running the app so that all parameters are taken from the newly created file instead of the basic application.yml. In order to run your tests using that configuration, simply type:
grails -Dgrails.env=myEnv test-app -functional
In brief, Grails 3 ships with an enhanced testing support, including functional testing. However, the file structure has changed and dealing with environments and configurations is different, though not harder at all.
STARTING WITH GRAILS3 POSTS