Assert regex matches in JUnit

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

    You can use Hamcrest and jcabi-matchers:

    import static com.jcabi.matchers.RegexMatchers.matchesPattern;
    import static org.junit.Assert.assertThat;
    assertThat("test", matchesPattern("[a-z]+"));
    

    More details here: Regular Expression Hamcrest Matchers.

    You will need these two dependencies in classpath:

    
      org.hamcrest
      hamcrest-core
      1.3
      test
    
    
      com.jcabi
      jcabi-matchers
      1.3
      test
    
    

提交回复
热议问题