Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to evaluate Spock @IgnoreIf using a method

Tags:

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: []

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

like image 365
orbfish Avatar asked Nov 07 '25 23:11

orbfish


1 Answers

The example is a little outdated Code written with Spock 0.7-groovy-2, if you want to use a static method then you need use the qualified version 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
    }
}

If you only want to run on windows, then there are some convenience functions built in (Spock 1.+).

  • Operating System @IgnoreIf({ os.windows || os.linux || os.macOs || os.solaris || os.other })
  • Java Version @IgnoreIf({ jvm.java5 || jvm.java6 || jvm.java7 || jvm.java8 || jvm.java9 })
  • Environment Variable @IgnoreIf({ !env.containsKey("FOOBARBAZ") }) @IgnoreIf({ env["FOOBARBAZ"] == 'false'})
  • SystemProperty @IgnoreIf({ !sys.contains("java.version") }) @IgnoreIf({ sys["java.version"] == '1.6' })
like image 164
Leonard Brünings Avatar answered Nov 11 '25 17:11

Leonard Brünings



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!