JUnit ClassRules in Spock

805 views
Skip to first unread message

grze...@gmail.com

unread,
May 12, 2015, 8:34:19 AM5/12/15
to spockfr...@googlegroups.com
Hi all

I can't find information if @ClassRule is respected bu spock during execution.

I try to write some class rule which can do some @BeforeClass initializations, but I can't make spock to respect it.

I came up with this :


class SputnikWithParametersTest extends Specification {

    @ClassRule
    @Shared
    public static ClassParameters<int[]> PARAMETERS = ClassParameters.withParameters([[2, 2], [-2, 2]])


    static int a;

    static int b;

    @BeforeClass
    void before() {
        int[] parameter = PARAMETERS.getParameter()
        println("Parameters : ${parameter}")
        a = parameter[0]
        b = parameter[1]
    }

    def 'some'() {
        when:
            int abs = Math.abs(a)
        then:
            abs == b
    }

    def 'second'() {
        when:
            int abs = Math.abs(a)
        then:
            abs == b
    }
}

but spock didn't catch Class Rule.


When I change it to normal rule :


class SputnikWithParametersTest extends Specification {

    @Rule
    ClassParameters<int[]> PARAMETERS = ClassParameters.withParameters([[2, 2], [-2, 2]])


    int a;

    int b;

    def 'some'() {
        setup:
            int[] parameter = PARAMETERS.getParameter()
            println("Parameters : ${parameter}")
            a = parameter[0]
            b = parameter[1]
        when:
            int abs = Math.abs(a)
        then:
            abs == b
    }

    def 'second'() {
        setup:
            int[] parameter = PARAMETERS.getParameter()
            println("Parameters : ${parameter}")
            a = parameter[0]
            b = parameter[1]
        when:
            int abs = Math.abs(a)
        then:
            abs == b
    }
}

it started to process rule (so it support normal rules)

Have anyone successfully used Class Rules with Spock ?



Regards,

Grzesiek

grze...@gmail.com

unread,
May 12, 2015, 8:52:08 AM5/12/15
to spockfr...@googlegroups.com
Hi,


actually mechanism is working, but I have some issue in my class rule, I need to debug it. Sorry for spamming,


Cheers,

Grzesiek

grze...@gmail.com

unread,
May 12, 2015, 8:57:46 AM5/12/15
to spockfr...@googlegroups.com
it seems that help change :
    static int a;

    static int b;

to 

    @Shared
    int a;

    @Shared
    int b;

it is probably of my leak of knowledge in groovy/spock beahvior.


Cheers,

Grzesiek
 

罗峰

unread,
Dec 1, 2017, 10:49:15 PM12/1/17
to Spock Framework - User
we need remove public static modifier and just keep   @ClassRule @Shared ClassParameters<int[]> ...
Reply all
Reply to author
Forward
0 new messages