How to evaluate Spock @IgnoreIf using a method

orb*_*ish 3 groovy spock

According to the following article:

https://dzone.com/articles/spocklight-ignore

you should be able to evaluate the Spock @IgnoreIf annotation using a static method. I tried the isOsWindows() method exactly as in the article, and got the following:

org.spockframework.runtime.extension.ExtensionException: Failed to evaluate @IgnoreIf condition
...
Caused by: groovy.lang.MissingMethodException: No signature of method: MySpec$__spock_feature_1_3_closure1.isOsWindows() is applicable for argument types: () values: []
Run Code Online (Sandbox Code Playgroud)

Does anyone have any insight on this? I really want to just set a true/false per spec, I don't need any of the os/system properties listed in the other examples.

Thank you

Leo*_*ngs 8

这个例子有点过时了Code written with Spock 0.7-groovy-2,如果你想使用静态方法,那么你需要使用合格的版本ClassName.method()

package com.mrhaki.spock

import spock.lang.*

class SampleRequiresSpec extends Specification {

    private static boolean isOsWindows() {
        System.properties['os.name'] == 'windows'
    }

    @IgnoreIf({ SampleRequiresSpec.isOsWindows() })
    def "run only if run on non-windows operating system"() {
        expect:
        true
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你只想在 Windows 上运行,那么有一些内置的便利功能(Spock 1.+)。

  • 操作系统 @IgnoreIf({ os.windows || os.linux || os.macOs || os.solaris || os.other })
  • 爪哇版 @IgnoreIf({ jvm.java5 || jvm.java6 || jvm.java7 || jvm.java8 || jvm.java9 })
  • 环境变量 @IgnoreIf({ !env.containsKey("FOOBARBAZ") }) @IgnoreIf({ env["FOOBARBAZ"] == 'false'})
  • 系统属性 @IgnoreIf({ !sys.contains("java.version") }) @IgnoreIf({ sys["java.version"] == '1.6' })