I have an issue with Spock, getting an assertions error when comparing identical Strings.
my test case:
<code>def "should return error when email already exists in company"() {
given: "A register driver request"
def requestBody = [
driverId: 12345L,
country: Country.DK,
firstName: "John",
lastName: "Doe",
companyId: "company123",
email: "[email protected]",
countryCallingCode: "1",
phoneNumber: "1234567890",
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")
.country(Country.DK)
.firstName("Jane")
.lastName("Doe")
.email(email)
.countryCallingCode("1")
.phoneNumber("0987654321")
.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}]")
}
</code>
<code>def "should return error when email already exists in company"() {
given: "A register driver request"
def requestBody = [
driverId: 12345L,
country: Country.DK,
firstName: "John",
lastName: "Doe",
companyId: "company123",
email: "[email protected]",
countryCallingCode: "1",
phoneNumber: "1234567890",
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")
.country(Country.DK)
.firstName("Jane")
.lastName("Doe")
.email(email)
.countryCallingCode("1")
.phoneNumber("0987654321")
.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}]")
}
</code>
def "should return error when email already exists in company"() {
given: "A register driver request"
def requestBody = [
driverId: 12345L,
country: Country.DK,
firstName: "John",
lastName: "Doe",
companyId: "company123",
email: "[email protected]",
countryCallingCode: "1",
phoneNumber: "1234567890",
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")
.country(Country.DK)
.firstName("Jane")
.lastName("Doe")
.email(email)
.countryCallingCode("1")
.phoneNumber("0987654321")
.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:
<code>Expected :Email [[email protected]] already exists in company [company123]
Actual :Email [[email protected]] already exists in company [company123]
</code>
<code>Expected :Email [[email protected]] already exists in company [company123]
Actual :Email [[email protected]] already exists in company [company123]
</code>
Expected :Email [[email protected]] already exists in company [company123]
Actual :Email [[email protected]] already exists in company [company123]
And when I use String literal in assertions, without ${}
placeholders, like here:
<code>"Driver ID [12345] already exists in company [company123]"
</code>
<code>"Driver ID [12345] already exists in company [company123]"
</code>
"Driver ID [12345] already exists in company [company123]"
it works fine.
Any solution to that issue?