Assert regex matches in JUnit

前端 未结 9 1238
广开言路
广开言路 2020-12-13 01:40

Ruby\'s Test::Unit has a nice assert_matches method that can be used in unit tests to assert that a regex matches a string.

Is there anythi

9条回答
  •  别那么骄傲
    2020-12-13 02:00

    If you use assertThat() with a Hamcrest matcher that tests for regex matches, then if the assertion fails you'll get a nice message that indicates expected pattern and actual text. The assertion will read more fluently also, e.g.

    assertThat("FooBarBaz", matchesPattern("^Foo"));
    

    with Hamcrest 2 you can find a matchesPattern method at MatchesPattern.matchesPattern.

提交回复
热议问题