Extension adds field at runtime

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Extension adds field at runtime

Jason Darby
We're migrating from TestNG to Spock for integration testing.  In our existing infrastructure we have a lot of utility closures that are similar to:

withNewThing { Thing thing ->
  thing.doSomething()
} //thing is cleaned up

I see that Spock has @AutoCleanup which is most of the functionality we're after but I don't want the resource to be instantiated and deleted for every test.  I know I can get the same functionality out of setup and cleanup blocks but I think an extension would be perfect for this, something like:

@WithThing
def "a test using the thing"() {
given:
thing.generateSomethingElse()
...

I've created an extension that can create and clean up a 'thing' but I need to provide a field to the test.  Is it possible(and practical)?  Am I going about this the wrong way?

Thanks!

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Extension adds field at runtime

Jason Darby
I was able to do it by creating an interceptor and adding a field to the metaclass of the current instance.

On Thursday, April 2, 2015 at 4:42:23 PM UTC-4, Jason Darby wrote:
We're migrating from TestNG to Spock for integration testing.  In our existing infrastructure we have a lot of utility closures that are similar to:

withNewThing { Thing thing ->
  thing.doSomething()
} //thing is cleaned up

I see that Spock has @AutoCleanup which is most of the functionality we're after but I don't want the resource to be instantiated and deleted for every test.  I know I can get the same functionality out of setup and cleanup blocks but I think an extension would be perfect for this, something like:

@WithThing
def "a test using the thing"() {
given:
thing.generateSomethingElse()
...

I've created an extension that can create and clean up a 'thing' but I need to provide a field to the test.  Is it possible(and practical)?  Am I going about this the wrong way?

Thanks!

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Extension adds field at runtime

Leonard Brünings
You just use
@Shared @AutoCleanup Thing thing = new Thing()

assuming, that the thing does not have to be created for fresh for every test that uses it.

On Saturday, April 4, 2015 at 9:49:54 PM UTC+2, Jason Darby wrote:
I was able to do it by creating an interceptor and adding a field to the metaclass of the current instance.

On Thursday, April 2, 2015 at 4:42:23 PM UTC-4, Jason Darby wrote:
We're migrating from TestNG to Spock for integration testing.  In our existing infrastructure we have a lot of utility closures that are similar to:

withNewThing { Thing thing ->
  thing.doSomething()
} //thing is cleaned up

I see that Spock has @AutoCleanup which is most of the functionality we're after but I don't want the resource to be instantiated and deleted for every test.  I know I can get the same functionality out of setup and cleanup blocks but I think an extension would be perfect for this, something like:

@WithThing
def "a test using the thing"() {
given:
thing.generateSomethingElse()
...

I've created an extension that can create and clean up a 'thing' but I need to provide a field to the test.  Is it possible(and practical)?  Am I going about this the wrong way?

Thanks!

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Extension adds field at runtime

Jason Darby
The functionality I was looking for was to have a fresh instance for individual tests without having to instantiate and set up for every test.  It would be more akin to having @AutoCleanup for a variable defined in given:

On Saturday, June 13, 2015 at 8:39:47 AM UTC-4, Leonard Brünings wrote:
You just use
@Shared @AutoCleanup Thing thing = new Thing()

assuming, that the thing does not have to be created for fresh for every test that uses it.

On Saturday, April 4, 2015 at 9:49:54 PM UTC+2, Jason Darby wrote:
I was able to do it by creating an interceptor and adding a field to the metaclass of the current instance.

On Thursday, April 2, 2015 at 4:42:23 PM UTC-4, Jason Darby wrote:
We're migrating from TestNG to Spock for integration testing.  In our existing infrastructure we have a lot of utility closures that are similar to:

withNewThing { Thing thing ->
  thing.doSomething()
} //thing is cleaned up

I see that Spock has @AutoCleanup which is most of the functionality we're after but I don't want the resource to be instantiated and deleted for every test.  I know I can get the same functionality out of setup and cleanup blocks but I think an extension would be perfect for this, something like:

@WithThing
def "a test using the thing"() {
given:
thing.generateSomethingElse()
...

I've created an extension that can create and clean up a 'thing' but I need to provide a field to the test.  Is it possible(and practical)?  Am I going about this the wrong way?

Thanks!

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.