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

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

问题描述

Ruby 的 Test::Unit 有一个很好的 assert_matches 方法,可以在单元测试中使用它来断言正则表达式匹配字符串.

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

使用 Hamcrest 2,您可以在 MatchesPattern.matchesPattern 中找到 matchesPattern 方法.

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

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

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