1

Unit test was working fine in its previous version while after updating the version it shows error

public class PersonSearchUtil {
    private final HashSet<SearchResponse> result = new HashSet<>();
    .
    .
    .
    public HashSet<SearchResponse> getResult() {
            return result;
    }
}

Test Method

@Test
fun `When person id is all zeroes, should merge`() {
    val uuid = UUID.randomUUID()
    add() {
        eeId = "same eeId"
        personId = UUID(0,0)
    }
    add() {
        eeId = "same eeId"
        personId = uuid
    }

    assertThat(service.result)
            .hasSize(1)
            .hasOnlyOneElementSatisfying { response ->.  // hasOnlyOneElementSatisfying is deprecated 
                assertThat(response.eeId).isEqualTo("same eeId")  // shows Unresolved reference: eeId 
                assertThat(response.personId).isEqualTo(uuid)  // shows Unresolved reference: personId
            }
}

Error:

Unresolved reference: eeId

Unresolved reference: personId

Jimmy
  • 995
  • 9
  • 18

1 Answers1

2

As mentioned in the javadoc, singleElement() should be used instead.

Also, there is a known issue with Kotlin and recent versions of AssertJ (assertj/assertj-core#2439).

One of the following workarounds might solve your issue until that gets addressed in AssertJ:

assertThat(service.result as Iterable<SearchResponse>)
    .hasSize(1)
    .hasOnlyOneElementSatisfying { response ->
        assertThat(response.eeId).isEqualTo("same eeId")
        assertThat(response.personId).isEqualTo(uuid)
    }
}
CollectionAssert(service.result)
    .hasSize(1)
    .hasOnlyOneElementSatisfying { response ->
        assertThat(response.eeId).isEqualTo("same eeId")
        assertThat(response.personId).isEqualTo(uuid)
    }
}
Stefano Cordio
  • 1,687
  • 10
  • 20