Assertion: assertLinesMatch()

Learn how to use the assertLinesMatch() method in JUnit 5.

The assertLinesMatch() method

The assertLinesMatch() method asserts that two lists of multiple String instances match. The matching algorithm used by assertLinesMatch() is more complicated than a simple string equality check. This is because output strings are usually templated and may be different in each test execution.

  1. The assertLinesMatch() method first checks using String.equals.
  2. If it doesn’t match, the expected string is treated as a regular expression and uses String.matches to match again.
  3. If it still doesn’t match, it checks if the expected string is a fast-forward marker. For fast-forward markers, the actual strings are fast-forwarded and run the check-in step 1 again.

A fast-forward marker starts and ends with >>. If the text between >> is an integer, it’s treated as the number of lines to skip. Otherwise, lines are continuously skipped until a match is found.

Get hands-on with 1200+ tech skills courses.