New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to use global GroovyMock in setupSpec() #791

Open
siyigu1 opened this Issue Nov 20, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@siyigu1

siyigu1 commented Nov 20, 2017

Issue description

When trying to setup some class globally using GroovyMock(i.e. GroovyMock(StringUtils, global: true)) in setupSpec, Spock throws exception saying:
java.lang.IllegalStateException: Cannot request current iteration in @Shared context.

After digging a little bit more, I found out that this is thrown here:

specification.getSpecificationContext().getCurrentIteration().addCleanup(new Runnable() {

And by reading the code, it seems to be caused by the fact that Spock is trying to attach a cleanup process to iteration which is failed because setupSpec is outside any test iteration.

I'm not entirely sure if this is as designed or a bug. I couldn't find anything on the documentation saying you are not supposed to use GroovyMock in setupSpec. Documentation is only specifying that you are not supposed to reference instance fields unless they are annotated with @shared which is clearly not the case here.

So I'm reporting it here and hoping it can be either fixed or at least the documentation be made clearer that this is a limitation one should not be doing.

How to reproduce

For example a simple test case like this:

class SomeTest extends Specification{

  def setupSpec() {
      GroovyMock(StringUtils, global: true)
      StringUtils.isEmpty(_) >> false
  }

  def "some test"() {
      expect:
      !StringUtils.isEmpty("")
  }
}

Will fail with the following message:

java.lang.IllegalStateException: Cannot request current iteration in @Shared context

	at org.spockframework.mock.runtime.GroovyMockFactory.create(GroovyMockFactory.java:49)
	at org.spockframework.mock.runtime.CompositeMockFactory.create(CompositeMockFactory.java:44)
	at org.spockframework.lang.SpecInternals.createMock(SpecInternals.java:45)
	at org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:281)
	at org.spockframework.lang.SpecInternals.GroovyMockImpl(SpecInternals.java:203)

Additional Environment information

Java/JDK

java version "1.8.0_151"

Groovy version

groovy 2.4.12

Operating System

Mac 10.12.6

IDE

Intellij

@leonard84

This comment has been minimized.

Show comment
Hide comment
@leonard84

leonard84 Nov 20, 2017

Member

It is not a bug, but maybe it should be mentioned somewhere. Mocks are always bound to a Specification instance and should not be shared. The setupSpec runs in a shared context and thus it fails. Do you have a specific use case why it would be necessary, or was this just confusion about why it doesn't work?
Just move your mock to setup and everything should work fine.

Member

leonard84 commented Nov 20, 2017

It is not a bug, but maybe it should be mentioned somewhere. Mocks are always bound to a Specification instance and should not be shared. The setupSpec runs in a shared context and thus it fails. Do you have a specific use case why it would be necessary, or was this just confusion about why it doesn't work?
Just move your mock to setup and everything should work fine.

@leonard84 leonard84 added the not a bug label Nov 20, 2017

@siyigu1

This comment has been minimized.

Show comment
Hide comment
@siyigu1

siyigu1 Nov 20, 2017

@leonard84 thanks for the explanation. That's what I thought as well. And I strongly agree with this should be mentioned somewhere in the documentation. Also the exception here should be clearer in reflecting what the real problem is. Right now saying java.lang.IllegalStateException: Cannot request current iteration in @Shared context. is very misleading

siyigu1 commented Nov 20, 2017

@leonard84 thanks for the explanation. That's what I thought as well. And I strongly agree with this should be mentioned somewhere in the documentation. Also the exception here should be clearer in reflecting what the real problem is. Right now saying java.lang.IllegalStateException: Cannot request current iteration in @Shared context. is very misleading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment