在JUnit中断言正则表达式匹配 [英] Assert regex matches in JUnit

查看:383
本文介绍了在JUnit中断言正则表达式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby的 Test :: Unit 有一个很好的 assert_matches 方法,可用于单元测试以断言a正则表达式匹配一个字符串。

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.

JUnit中有这样的东西吗?目前,我这样做:

Is there anything like this in JUnit? Currently, I do this:

assertEquals(true, actual.matches(expectedRegex));


推荐答案

如果你使用 assertThat( )使用 Hamcrest匹配器测试正则表达式匹配,如果断言失败,你会收到一条表明预期模式和实际文本的好消息。断言也会更流利,例如。

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"));

这篇关于在JUnit中断言正则表达式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆