Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 anything like this in JUnit? Currently, I do this:

assertEquals(true, actual.matches(expectedRegex));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
941 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...