spockframework / spockframework/spock

Not able to use global GroovyMock in setupSpec()

Open
#791 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

not a bug
Dominant language
Java
Stars
3.6k
Forks
483
PR merge metrics
No merged PRs in 30d

Description

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: https://github.com/spockframework/spock/blob/6ca84d59da57a0186910daf88fce7d5e1cbb2dfa/spock-core/src/main/java/org/spockframework/mock/runtime/GroovyMockFactory.java#L50
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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the setupSpec() reproducer in the issue and inspect spock-core/src/main/java/org/spockframework/mock/runtime/GroovyMockFactory.java around line 50. Trace why global GroovyMock requests the current iteration during shared setup. Done means the reproducer works as intended, or the supported limitation is clearly documented with corresponding test coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy, java
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.