2014-04-11

Spock test template for IntelliJ IDEA

Spock tests should have specific structure. I generally use one form of test: with given, when, then and optional where blocks. I also like to have all cases visible, so I add Unroll annotation. I need simple way to generate test in this way.

I am developing using IntelliJ IDEA, so I describe how to configure live template using this IDE. Select File->Settings...->Live Templates. Now add new template group and add new template inside this. Abbreviation set to something meaningful and short, for example spock. As template text use code below:
@spock.lang.Unroll
def "should $END$"(){
    given:
    
    when:
    
    then:
    
    where:
    
}
Set also option "Reformat according to style", apllicable set to groovy and choose expand key.

Now, when you type spock and click expand key (in my case it is Tab) in editor, then spock will be replaced with template and coursor will be set after space in test method name.

1 comment: