javagroovyspock

Spock - issue with assertions when using placeholders in GString


I have an issue with Spock, getting an assertions error when comparing identical Strings.

my test case:

def "should return error when email already exists in company"() {
    given: "A register driver request"
    def requestBody = [
        driverId: 12345L,
        companyId: "company123",
        email: "john.doe@example.com",
        isActive: true
    ]

    def email = requestBody.email
    def companyId = requestBody.companyId

    and: "Insert a driver with the same email into the database"
    driverRepository.save(DriverEntity.builder()
        .id("uniqueId1")
        .companyId(companyId)
        .driverId("67890")
        .email(email)
        .build())

    when: "The register driver endpoint is called"
    def response = webTestClient.post()
        .uri("/drivers")
        .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
        .bodyValue(requestBody)
        .exchange()

    then: "The response indicates a bad request due to duplicate email"
    response.expectStatus().isEqualTo(422)
        .expectBody()
        .jsonPath("\$.error").isEqualTo("EMAIL_ALREADY_EXISTS")
        .jsonPath("\$.description").isEqualTo("Email [${email}] already exists in company [${companyId}]")

  } 

But Strings are identical:

Expected :Email [john.doe@example.com] already exists in company [company123]
Actual   :Email [john.doe@example.com] already exists in company [company123]

And when I use String literal in assertions, without ${} placeholders, like here:

"Driver ID [12345] already exists in company [company123]"

it works fine.

Any solution to that issue?


Solution

  • Whatever that isEqualTo is coming from, it probably does not have special handling for Groovy GStrings like Groovy itself has.

    If you for example check this script:

    def foo = 'a'
    assert 'a' == "$foo"
    assert !'a'.equals("$foo")
    assert 'a'.equals("$foo" as String)
    

    then you see that for Groovy the String and the GString are equal.
    But if you use equals, then they are different.
    And that most probable is, what isEqualTo is testing.

    So if you just add as String after your GString it should early resolve the placeholders and make it a normal String that will pass the comparison.


    As long as you do not have any placeholders in your GString it will actually be compiled to a regular String even if you used double-quotes. But I prefer to always use single-quotes, explicitly making it a String if I don't want any replacements done. This can also improve compilation speed as the GString must not be checked for contained placeholders but can simply be taken literally.

    There are even more ways to express strings in Groovy, like /foo\bar/, where the backslash is not an escape character. This again is a GString though if you contain placeholders. For more information see https://groovy-lang.org/syntax.html#all-strings.