Search code examples
grailsintellij-ideabddspockeasyb

"Test a little, code a little" in Grails project, using Easyb or Spock, in IntelliJ


My original issue is described perfectly by this post: I want to follow TDD:

  • write a small test
  • watch it fail
  • write just enough code to make it succeed
  • watch it succeed
  • repeat

I am working on a Grails project in IntelliJ. If all I want is to write normal JUnit tests, the above post solves everything:

  • Head to /test/unit
  • Put some test code in a "class Xyz extends GroovyTestCase" class
  • Hit Shift F10
  • JUnit report pops up within a second or two

The problem is that I would like to use one of the very cool "describe-in-english" testing setups, like Easyb or Spock.

What do I do? It would be magic to just start with the auto-generated Test class Grails makes for me, then cram Spock stuff into it. Obviously I can't use "extends" twice. Does this give the gist of what I'm trying to do though?

class Xyz extends GroovyTestCase extends spock.lang.Specification {

    //void testSomething() {
    //    fail "Implement me"
    //}

    def "length of Spock's and his friends' names"() {
        expect:
        name.size() == length

        where:
        name     | length
        "Spock"  | 5
        "Kirk"   | 4
        "Scotty" | 6
    }
}

Solution

  • Extend spock classes, not groovy's. You can choose from UnitSpec, ControllerSpec, IntegrationSpec and others as listed in source code. Spock will take care of the rest.