Java 在 jUnit 中的字符串上的 AssertContains

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1092219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:35:14  来源:igfitidea点击:

AssertContains on strings in jUnit

javastringjunitassert

提问by ripper234

Is there a nicer way to write in jUnit

有没有更好的方法在 jUnit 中编写

String x = "foo bar";
Assert.assertTrue(x.contains("foo"));

采纳答案by Yishai

If you add in Hamcrest and JUnit4, you could do:

如果添加 Hamcrest 和 JUnit4,则可以执行以下操作:

String x = "foo bar";
Assert.assertThat(x, CoreMatchers.containsString("foo"));

With some static imports, it looks a lot better:

使用一些静态导入,它看起来好多了:

assertThat(x, containsString("foo"));

The static imports needed would be:

所需的静态导入是:

import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.containsString;

回答by Robert Munteanu

Use the new assertThatsyntax together with Hamcrest.

将新assertThat语法与Hamcrest一起使用。

It is available starting with JUnit 4.4.

它从JUnit 4.4开始可用。

回答by piotrek

use fest assert 2.0whenever possible EDIT: assertjmay have more assertions (a fork)

尽可能使用fest assert 2.0编辑:assertj可能有更多断言(一个叉子)

assertThat(x).contains("foo");

回答by Mike Rylander

Use hamcrest Matcher containsString()

使用 hamcrest Matcher containsString()

// Hamcrest assertion
assertThat(person.getName(), containsString("myName"));

// Error Message
java.lang.AssertionError:
Expected: a string containing "myName"
     got: "some other name"

You can optional add an even more detail error message.

您可以选择添加更详细的错误消息。

// Hamcrest assertion with custom error message
assertThat("my error message", person.getName(), containsString("myName"));

// Error Message
java.lang.AssertionError: my error message
Expected: a string containing "myName"
     got: "some other name"

Posted my answer to a duplicate question here

在这里发布了我对重复问题的回答

回答by LEON DENIS

Another variant is

另一种变体是

Assert.assertThat(actual, new Matches(expectedRegex));

Moreover in org.mockito.internal.matchersthere are some other interesting matchers, like StartWith, Containsetc.

此外,在org.mockito.internal.matchers有一些其他有趣的匹配,比如StartWithContains等等。

回答by P Kuijpers

I've tried out many answers on this page, none really worked:

我在这个页面上尝试了很多答案,但没有一个真正有效:

  • org.hamcrest.CoreMatchers.containsStringdoes not compile, cannot resolve method.
  • JUnitMatchers.containsStringis depricated (and refers to CoreMatchers.containsString).
  • org.hamcrest.Matchers.containsString: NoSuchMethodError
  • org.hamcrest.CoreMatchers.containsString无法编译,无法解析方法。
  • JUnitMatchers.containsString弃用(并引用CoreMatchers.containsString)。
  • org.hamcrest.Matchers.containsString: NoSuchMethodError

So instead of writing readable code, I decided to use the simple and workable approach mentioned in the question instead.

因此,我决定使用问题中提到的简单可行的方法,而不是编写可读的代码。

Hopefully another solution will come up.

希望另一个解决方案会出现。

回答by rns

It's too late, but just to update I got it done with below syntax

为时已晚,但只是为了更新我使用以下语法完成了

import org.hamcrest.core.StringContains;
import org.junit.Assert;

Assert.assertThat("this contains test", StringContains.containsString("test"));

回答by Lazarenko Alex

assertj variant

assertj 变体

import org.assertj.core.api.Assertions;
Assertions.assertThat(actualStr).contains(subStr);