1

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
    }
}
Bukov
  • 650
  • 8
  • 19
  • 1
    A few more days of researching TDD later: it looks like there's another important step before the "repeat" at the end. "Refactor". You write just enough code to make the test succeed, even if the code is janky. Once test succeeds, you go back and "pretty up" the code, testing frequently to make sure you're not breaking it – Bukov Jun 01 '12 at 01:55

2 Answers2

3

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.

Tomasz Kalkosiński
  • 3,673
  • 1
  • 19
  • 25
  • Note that with Grails 2.0, you no longer use `UnitSpec` etc. but just `spock.lang.Specification` together with the new Grails test annotations. – Peter Niederwieser May 29 '12 at 06:37
  • Thank you both! You were correct. I changed my Grails Test Templates to be "class Xyz extends Specification {}" then just run it as a JUnit test. Everything works like magic. – Bukov Jun 01 '12 at 01:33
1

-Use "grails install-templates"

-Change the Tests templates to something along the lines of:

@artifact.package@

import grails.test.mixin.*
import org.junit.*
import spock.lang.Specification

@TestFor(@artifact.testclass@)
class @artifact.name@ extends Specification {

}

-Write the test code in Spock (or normal JUnit code)

-IntelliJ->Run->Edit Configurations. Add New Configuration of the JUnit type. Test kind: <All in package/All in UnitTest directory/All in one UnitTest class/etc.>

-(Shortcut: Cursor over method name, or class name->ctrl-shift-F10)


On my original question: In retrospect, I was getting hung-up on the "run all in directory" part of that blog post. Current IntelliJ DOES let you run all JUnit tests in a directory, or the entire project.

Once I understood that, the next step was to realize that Spock tests ARE JUnit tests. Make a "class Xyz extends Specification {}" class in the test/unit directory, fill it with Spock code, and Run as... JUnit. Magic!

Bukov
  • 650
  • 8
  • 19