Spock executes superclass's "setup" method overridden by subclass. Is it expected work?

3,218 views
Skip to first unread message

literalice

unread,
Dec 27, 2009, 11:02:48 AM12/27/09
to Spock Framework
Hello,

I have created a spec as subclass of another spec,
and overridden "setup" method of supers.
I have expected super.setup method is not executed.
But, maybe Spock have executed super.setup method in spite of I have
not called super.setup explicitly.
Is it expected Spock work?
If so, could I kill super.setup method action?

I have created a spec like below.

class BaseSpock extends Specification {
def setup() {
println "BaseSpock running..."
}
}
class HelloSpock extends BaseSpock {
@Override
def setup() {
println "HelloSpock running..."
}
def "can you figure out what I'm up to?"() {
expect:
"Spock".size() == 5
}
}

And, System output is here.
------------
T E S T S
------------
Running HelloSpock
BaseSpock running...
HelloSpock running...
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.969
sec
Running BaseSpock
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec

I use
* groovy 1.7.0
* spock 0.4-groovy-1.7-SNAPSHOT

Regards

Peter Niederwieser

unread,
Dec 27, 2009, 4:08:15 PM12/27/09
to spockfr...@googlegroups.com
Yes, it works as intended. Like constructors, setup() methods cannot be overridden and are always run. Which concrete problem are you trying to solve?

Cheers,
Peter

> --
>
> You received this message because you are subscribed to the Google Groups "Spock Framework" group.
> To post to this group, send email to spockfr...@googlegroups.com.
> To unsubscribe from this group, send email to spockframewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.
>
>

literalice

unread,
Dec 27, 2009, 7:45:08 PM12/27/09
to Spock Framework
Thank you for your reply,
I got to understand Spock Specification.

I've not had a concrete problem.
Just, I use base specification class that extends Grails's
"UnitSpecification"(defining setup()), and I was not sure I should
call super.setup() or not when I define my setup() method in the base
specification.
In addition, I had a question about what to do in case I don't need
UnitSpecification.setup() (maybe, such a situation unlikely to be
encountered...).


Thank you,

Peter Niederwieser

unread,
Dec 27, 2009, 8:56:09 PM12/27/09
to Spock Framework
On Dec 28, 1:45 am, literalice <literal....@gmail.com> wrote:
> Just, I use base specification class that extends Grails's
> "UnitSpecification"(defining setup()), and I was not sure I should
> call super.setup() or not when I define my setup() method in the base
> specification.

You don't have to call super.setup(). Actually, we should flag this as
an error, as it would result in the base method being run twice.

> In addition, I had a question about what to do in case I don't need
> UnitSpecification.setup() (maybe, such a situation unlikely to be
> encountered...).

Let us know in case you encounter such a situation. (If you are in
control of the base class, this is easy to achieve: Let the base setup
() method delegate to doSetup(), and override doSetup() in the derived
class. But think twice before doing this. There might be a better
solution to your problem.)

Cheers,
Peter

João Peixoto

unread,
Mar 21, 2013, 8:10:29 PM3/21/13
to spockfr...@googlegroups.com
I know this answer has some time now but I was wondering if you could elaborate that doSetup strategy.

In my scenario I have several tests for regular users. My setup method creates the user in the database and "logs in" before each test.
Now, Admin users have the same features as regular ones plus some, so I want to run all regular user tests with an admin user plus whatever additional tests make sense.

Unfortunately the setup methods gets run 2 times and the super class takes precedence, so a regular user will always be present first.

This doSetup delegation seems like the ideal strategy but not sure how to achieve it

Any tips?

Thanks in advance

Peter Niederwieser

unread,
Apr 8, 2013, 2:37:04 AM4/8/13
to spockfr...@googlegroups.com
You'd simply have a `setup` method in the base class that calls, say, a `doSetup` method. The latter would be overridden by subclasses.

Cheers,
Peter
Reply all
Reply to author
Forward
0 new messages