Grails Interview Questions

Grails Interview Questions

Talk at Greach 2017


Sergio del Amo

March 31, 2017



  2. Sergio del Amo @sdelamo

  3. Welcome! Let's talk about your Grails experience

  4. Which build tool does Grails use?


  6. Tell me about Grails Scaffolding

  7. Scaffolding is a Grails feature that allows you to

    quickly generate CRUD interfaces for an existing domain
  8. DYNAMIC SCAFFOLDING dependencies { // ... compile "org.grails.plugins:scaffolding" //

    ... } build.gradle class Book { String name String about } grails-app/domain class BookController { static scaffold = Book } grails-app/controllers class BookController { static scaffold = true } Old Grails 2.x way
  9. STATIC SCAFFOLDING grails generate-controller Book grails generate-views Book grails

    generate-all Book Generate a controller and the views for a domain class
  10. Given this scaffolding snippet. Take a look at the

    use of DELETE. Did not browsers support only GET and POST for forms?
  11. Actually POST is used. DELETE is passed as a

    parameter named _method.Request gets intercepted by which wraps the request and uses DELETE AWESOME! +1
  12. Tell me about Grails environments

  13. Grails supports the concepts of development environments by default

    and will configure itself accordingly when executed.
  14. ENVIRONMENTS environments: development: dataSource: dbCreate: create-drop url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE test:

    dataSource: dbCreate: update url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE production: dataSource: dbCreate: none url: jdbc:h2:./prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE application.yml
  15. CONFIGURATION FILES PER ENVIRONMENT grails-app conf application-production.yml application-development.yml application-test.yml

    Checkout mrhaki blog post: Using External Configuration files per environment
  16. Execute Gradle tasks per environment ./gradlew -Dgrails.env=prod bootRun

  17. Pass System Properties bootRun { systemProperties } integrationTest

    { systemProperties }
  18. Do you know what the acronym ORM stands for?

  19. Stands for Object-Relational Mapping. That it is to say:

    Mapping classes onto a relational database layer
  20. In GRAILS we normally use GORM. A data access

    toolkit which provides a rich set of APIs for accessing relational and non-relational data
  21. Which GORM implementations do you know about?

  22. GORM Implementations

  23. In the previous scaffolding code I found this method:

    def index(Integer max) { params.max = Math.min(max ?: 10, 100) respond Book.list(params), model:[bookCount: Book.count()] } I have not defined list() or count() methods. What is going on?
  24. Those are Dynamic Finders. A dynamic finder looks like

    a static method invocation, but the methods themselves don’t actually exists in any form at the code level. Instead, a method is generated for us based on the properties of a given class.
  25. Do you know what a derived property is?

  26. A derived property is one that takes its value

    from a SQL expression, often but not necessarily based on the value of one or more persistent properties
  27. What about using a transient instead the derived property?

  28. With a derived property you have the ability to

    do queries such as: Product.findAllByTaxGreaterThan(21.12) The drawback of derived queries is that you may give some database portability
  29. In Grails applications you don’t see a lot of

    try / catch block. Why?
  30. Groovy translates all exceptions into runtime exceptions, so Groovy

    code is never forced to catch an exception
  31. Any area of Grails development where the distinction Checked

    and Unchecked exceptions is important?
  32. Transactional methods get an automatic rollback if a method

    throws a runtime exception. Checked exceptions do NOT rollback transactions
  33. I am developing a Grails application to organise my

    agenda. I have a domain class: class AgendaEvent { String eventName Date eventDate } I want to restrict the application to enter only upcoming dates. Could you help me?
  34. No problem! A Grails domain class can express constraints

    simply by defining a static property named constraints, that has a closure as a value
  35. Constraints - Custom validator

  36. Any other area of Grails where you can often

    use validators?
  37. Yes, Command Objects and URLMappings

  38. I wrote a test with for your validator. Do

    you see any problem with it?
  39. Replace validate() with no args with validate with a

    List of properties which are under test. Thus, you test eventDate on isolation without the need to provide valid values for other properties
  40. I personally would use a where block in your

    Spock test.
  41. In a Grails Controller, I have such an action:

    Do you see any problems?
  42. Params object has a number of convenience methods for

    boolean, long, char, short …
  43. You could use the parameters as action method arguments.

    If you provide wrong types default values will be binded. e.g. null, false
  44. However, the best approach would probably be to use

    a Command Object.
  45. I want to display the last five books entered

    into the system. What do you think?
  46. grails-app/controllers/demo/BookController.groovy

  47. grails-app/conf/application.yml

  48. encapsulate your business logic in Grails services grails-app/services/demo/BookService.groovy

  49. grails-app/views/book/latestBooks.gsp

  50. Why are you getting the configuration values implementing GrailsConfigurationAware

    instead of using grailsApplication?
  51. It is not ideal because the application is paying

    the performance price of retrieving the config value every time the service method is invoked. Services are singletons by default. void setConfiguration(Config co) will be called just once
  52. Could you write the service with a where query

  53. grails-app/services/demo/BookService.groovy

  54. •Use DetachedCriteria •Compile time type checked •Compatible with static compilation

    •Use raw Groovy for criteria •Are compose-able WHERE QUERIES
  55. I want to restrict the invocations of a controller’s

    action to DELETE requests. I plan to create an interceptor and reject any request with a HTTP verb other than DELETE. What do you think?
  56. It is much easier! You should use Grails allowedMethods

    property in your controller!
  57. Do you know how to create a multi-project build

    with Grails 3?
  58. WHAT ABOUT MULTI-PROJECT dependencies { // ... compile project(“:myplugin”)

    // ... } myapi/build.gradle grails create-app myapi —profile=rest-api grails create-plugin myplugin --profile=plugin CREATE AN APP AND A PLUGIN include 'myapi', 'myplugin' settings.gradle
  59. Can you tell me about Grails profiles?

  60. •angular - A profile for creating applications using AngularJS •rest-api

    - Profile for REST API applications •base - The base profile extended by other profiles •angular2 - A profile for creating Grails applications with Angular 2 •plugin - Profile for plugins designed to work across all profiles •profile - A profile for creating new Grails profiles •react - A profile for creating Grails applications with a React frontend •rest-api-plugin - Profile for REST API plugins •web - Profile for Web applications •web-jboss7 - A Profile for Creating a JBoss 7.1 EAP Project •web-plugin - Profile for Plugins designed for Web applications •webpack - A profile for creating applications with node-based frontends using webpack
  61. What would you upgrade an app from Grails 3.2.6

    to 3.2.8?
  62. I would checkout the release notes in Normally

    it involves increasing a version number in I would diff two apps created from scratch with each version? Or use a grails-versions
  63. How do you keep learning about Grails?

  64. I checkout every Monday

  65. I am subscribed to

  66. Do you know how to get commercial support for



  69. ?

  70. OCI 12140 Woodcrest Exec. Dr., Ste. 250 Saint Louis, MO

    63141 USA © 2017, All Rights Reserved. No part of this publication may be photocopied or reproduced in any form without written permission from OCI. Nor shall the OCI logo or copyright information be removed from this publication. No part of this publication may be stored in a retrieval system, transmitted by any means, recorded or otherwise, without written permission from OCI. While every precaution has been taken in preparing this material, including research, development and testing, OCI assumes no responsibility for errors or omissions. No liability is assumed by OCI for any damages resulting from the use of this information.