Showing posts with label Grails3. Show all posts
Showing posts with label Grails3. Show all posts

Wednesday, October 26, 2016

Side effects with not having decent UNIT tests

This is not Grails/Groovy specific but in general.

Grails/Groovy is certainly more reliant on tests, as it is dynamically typed, so code needs to be executed to pickup errors. The Java world seems to rely on static typing while Groovy on tests. Tests are always better than typing, period.

Besides the obvious, if it is not tested how do you know it works? There are some interesting side effects I see with poorly unit tested code.

Makes your Code Better

  • Having to write unit tests for a class/method makes your code structurally better.
  • It leads to better separation of concerns. 
  • Starts enforcing the law of Demeter. 
  • Easily tested code is generally easier to understand. 


Documents Code

  • Documentation falls out of date and quickly becomes irrelevant. Properly written tests (Spock helps) is a great way to document code. 


Makes changing existing code possible

  • I cannot change existing if I have no idea if it works, or how it works. 
  • In larger systems knowing if existing code works without tests becomes increasingly difficult.
  • It can be very difficult to execute see what code is doing if you need to stand up the whole system just to test a method.


Quicker to Change and Safer to Change

  • If your code has a unit test you can execute it immediately.
  • You can see changes, and what they do to existing tests immediately
  • 100's of times an hour.

Friday, October 14, 2016

Grails3 GORM running MongoDB

Had some issues getting Mongo to work with Grails3 based on this doc, ended up creating an example project of it all working

Feel free to download, have a look

Spock data driven testing with interactions (Mocks)

You can do data driven testing with Mocks and interactions, the key is that your interaction should be in the setup: block For Example
    @Unroll
    def "createOtherField test to see if label override works #fieldName override(#fieldLabel) == #resultName"() {
        setup:
        FormFieldMapManager formFieldMapManager = Mock()
        String onlineFormId = '1'
        List<LabelValue> otherFields = []
        1*formFieldMapManager.getMappedFields(onlineFormId, false) >> [new FormFieldMap()]

        expect:
        List<LabelValue> result = ProcessorUtil.createOtherField(formFieldMapManager, onlineFormId, otherFields, fieldName, 'fieldValue')
        result[0].label == resultName

        where:
        fieldName   | fieldLabel    || resultName
        'fieldName' | null          || 'fieldName'
        'fieldName' | ''            || 'fieldName'
        ''          | null          || ''
        'fieldName' | 'OVERRIDE'    || 'OVERRIDE'
    }

Sunday, September 11, 2016

Grails logging debug from your controller logback.groovy

To get your controllers and services logging debug messages edit your logback.groovy. Just add your package structure, for instance if your package structure was com.myapp

root(ERROR, ['STDOUT']) // Sets log level of all
logger('grails.app.controllers.com.myapp', DEBUG, ['STDOUT'], false) // Turn on debug for all your controllers
logger('grails.app.services.com.myapp', DEBUG, ['STDOUT'], false) // Turn on debug for all your services