1

I would like to use PollingConditions.eventually to evaluate and return value:

    @Shared
    static PollingConditions POLLING_CONDITIONS = new PollingConditions(
            timeout: 30, delay: 1.5, factor: 1.25
    )

    MyResponseContainerDto sendRequestWithRetries(String body, String uri) {
        ExtractableResponse<Response> extractedResponse = null
        POLLING_CONDITIONS.eventually {
           extractedResponse = sendRequest(body, uri).extract()
            assert extractedResponse.statusCode() == 200
        }

        return extractedResponse.as(MyResponseContainerDto.class)
    }

    ValidatableResponse sendRequest(String body, String uri) {
            return RestAssured.given()
                    .contentType(JSON)
                    .body(body)
                    .when()
                    .post("/myApi" + "/" + uri)
                    .then()
                    .log().all()
    }

When I try to run the above code I get:

Expected a condition, but found an assignment. Did you intend to write '==' ? @ line 42, column 12.
              extractedResponse = sendRequest(body, uri).extract()
              ^

Is there a possibility to have an assignment inside the eventually block?

Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22
pixel
  • 24,905
  • 36
  • 149
  • 251

2 Answers2

2

As an alternative to Marcin's suggestion, a simple workaround would be an explicit interaction block:

package de.scrum_master.stackoverflow.q75663270

import spock.lang.Shared
import spock.lang.Specification
import spock.util.concurrent.PollingConditions

class PollingConditionsTest extends Specification {
  @Shared
  PollingConditions POLLING_CONDITIONS = new PollingConditions(
    timeout : 3, delay : 0.2, factor : 1
  )

  def myHelper() {
    def extractedResponse = null
    POLLING_CONDITIONS.eventually {
      interaction {
        extractedResponse = "xxx"
        assert extractedResponse.contains("x")
      }
    }
    extractedResponse
  }

  def test() {
    expect:
    myHelper() == "xxx"
  }
}

Please note however that, because the interaction block in your case is not inside then/expect but rather in a helper method, you still need the explicit assert.

Try it in the Groovy Web Console.

P.S.: A @Shared field should not be static. It is Spock's better (with regard to Spock spec lifecycle) alternative to static fields.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
1

Quoting the Spock lead maintainer (from 2018, but it seems to still hold):

(...) assignments (other than to a new variable) in blocks with implicit assertions are forbidden for the expressed purpose of preventing the common mistake of forgetting one = for an equality check, and this will not be changed.

The eventually (and within) methods in PollingConditions are marked as @ConditionBlock and treated the same way as the then: and except: blocks.

As a (not very readable) workaround, you might replace the assignment with some "container". Here, an example with AtomicReference (available in JDK), but any "custom container" will be enough:

    @Shared
    PollingConditions POLLING_CONDITIONS = new PollingConditions(
            timeout: 30, delay: 1.5, factor: 1.25
    )

    MyResponseContainerDto sendRequestWithRetries(String body, String uri) {
        AtomicReference<ExtractableResponse<Response>> extractedResponseContainer = 
             new AtomicReference<>()

        POLLING_CONDITIONS.eventually {
            extractedResponse.set(sendRequest(body, uri).extract())
            assert extractedResponseContainer.get().statusCode() == 200
        }

        return extractedResponseContainer.get().as(MyResponseContainerDto.class)
    }

    ValidatableResponse sendRequest(String body, String uri) {
            return RestAssured.given()
                    .contentType(JSON)
                    .body(body)
                    .when()
                    .post("/myApi" + "/" + uri)
                    .then()
                    .log().all()
    }

Alternatively, you might use Awaitility - which is less strict in that field - to perform "active waiting". For example with the approach proposed in that answer.

Marcin Zajączkowski
  • 4,036
  • 1
  • 32
  • 41