Assert regex matches in JUnit

前端 未结 9 1248
广开言路
广开言路 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:03

    Because I was also looking for this functionality, I have started a project on GitHub called regex-tester. It's a library that helps ease testing regular expressions in Java (only works with JUnit currently).

    The library is very limited right now, but it does have a Hamcrest matcher that works like this

    assertThat("test", doesMatchRegex("tes.+"));
    assertThat("test", doesNotMatchRegex("tex.+"));
    

    More about how to use regex-tester is here.

提交回复
热议问题