在assertEquals()中使用正则表达式不起作用。

bluesky1年前 ⋅ 101 阅读

ChatGPT的回答仅作参考:

是的,JUnit的assertEquals()方法不支持正则表达式。它只是简单地比较两个对象是否相等。如果您想使用正则表达式进行比较,可以使用JUnit的assertThat()方法和Hamcrest库中的正则表达式匹配器。例如:

```
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

...

String actual = "Hello, world!";
String expectedRegex = "Hello, .*!";
assertThat(actual, matchesPattern(expectedRegex));
```

这将使用Hamcrest的matchesPattern()匹配器来比较实际字符串是否与预期的正则表达式匹配。

全部评论: 0

    相关推荐