0

I have an actual list of strings generated in my integration test and an expected list of substrings. It is trivial to assert that the collections are equal, e.g.:

assertThat(actual).containsExactly(expected);

In my case it is a bit more difficult though, because I would effectively like to have a containsExactlySubstring() function -- that is I would like to assert that there is a one-to-one correspondence between the actual strings and expected substrings. Is there a neat (descriptive) way to implement it out of the box?

EXAMPLES:

expected = {"abc", "def", "ghi"}
actualPass = {"#abc", "#ghi", "#def"}
actualFail1 = {"abc", "def"}
actualFail2 = {"#abc", "#ghi", "#abc"}
actualFail3 = {"#abc", "#ghi", "#xyz"}
Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72
Grzenio
  • 35,875
  • 47
  • 158
  • 240
  • Can you show us an example of what is expected, what you mean by "substring" is unclear –  Nov 26 '15 at 07:30
  • @RC. thanks for the comment, added a few examples – Grzenio Nov 26 '15 at 07:35
  • 3
    It's unlikely there would be an off-the-shelf comparator for such a contrived case, but it should be fairly easy to write a custom assertion, right? – kryger Nov 26 '15 at 13:15

1 Answers1

1

You can either use a condition that would be verified on all elements using are(condition) / have(condition) or as kryger suggested use an element comparator.

Hope it helps.

Community
  • 1
  • 1
Joel Costigliola
  • 6,308
  • 27
  • 35